Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within EJB and other Jakarta /Java EE Technologies
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Tim Cooke
Campbell Ritchie
paul wheaton
Ron McLeod
Devaka Cooray
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Paul Clapham
Saloon Keepers:
Tim Holloway
Carey Brown
Piet Souris
Bartenders:
Forum:
EJB and other Jakarta /Java EE Technologies
How can i call/execute the "@configuration" java class in spring project?
Arun Singh Raaj
Ranch Hand
Posts: 240
2
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello guys,
I don't know how to invoke the configuration file in spring-data jpa based application. How does this file execute by spring container when the application starts?
thanks in advance.
Here is the code of that file=
@Configuration @EnableTransactionManagement @EnableJpaRepositories(basePackages = { "Persistence_Layer" }) class PersistenceContext { //Configure the required beans here @Bean(destroyMethod = "close") DataSource dataSource(Environment env) { DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName(env.getRequiredProperty("db.driver")); ds.setUrl(env.getRequiredProperty("db.url")); ds.setUsername(env.getRequiredProperty("db.username")); ds.setPassword(env.getRequiredProperty("db.password")); return ds; } //configure EntityManager @Bean LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, Environment env) { LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); entityManagerFactoryBean.setDataSource(dataSource); entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); entityManagerFactoryBean.setPackagesToScan("Business_Layer"); Properties jpaProperties = new Properties(); //Configures the used database dialect. This allows Hibernate to create SQL //that is optimized for the used database. jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect")); //Specifies the action that is invoked to the database when the Hibernate //SessionFactory is created or closed. jpaProperties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto") ); //Configures the naming strategy that is used when Hibernate creates //new database objects and schema elements jpaProperties.put("hibernate.ejb.naming_strategy", env.getRequiredProperty("hibernate.ejb.naming_strategy") ); //If the value of this property is true, Hibernate writes all SQL //statements to the console. jpaProperties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql") ); //If the value of this property is true, Hibernate will format the SQL //that is written to the console. jpaProperties.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql") ); entityManagerFactoryBean.setJpaProperties(jpaProperties); return entityManagerFactoryBean; } //transaction manager bean @Bean JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(entityManagerFactory); return transactionManager; } //Add the other beans here }
Daniel Demesmaecker
Rancher
Posts: 1171
18
I like...
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class, Config.class); ctx.refresh(); MyService myService = ctx.getBean(MyService.class); myService.doStuff(); }
It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
Arun Singh Raaj
Ranch Hand
Posts: 240
2
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
thanks Daniel
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
can not scan the entity defined in orm.xml file
configuration files in spring boot
how to attach entity in JAX-RS service?
Connect to database with hibernate and spring
Need inputs for Database Tuning using Spring and Dbcp Connection pool
More...