My recent goal is to build a spring boot application, but without any XML config files (or as less as possible) so I would like to avoid using some XML files (i.e. web.xml) especially for some bean definition parts.
And here comes tougher part.
I want to inject using @Autowired annotation a SessionFactory bean into classes, but everytime I try to start application I get:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'temperatureController': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: EntityManagerFactory must not be null
Ok, I understand that Spring has no SessionFactory bean because it has no EntityManagerFactory.
So I would appreciate any suggestions how to solve this, but only with configuration by annotations.
So far I read similar post to mine about specifying in @Configuration class a bean this way:
@Bean
public HibernateJpaSessionFactoryBean sessionFactory() {
return new HibernateJpaSessionFactoryBean();
}
And then adding this line into properties file:
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
And finally @Autowired with SessionFactory should work good. But, of course for me it's not working.
Any ideas what should I do different/better?
My properties file is very basic:
spring.jpa.show-sql = true spring.datasource.password=mysql spring.datasource.username=mysql spring.datasource.testWhileIdle = true spring.jpa.hibernate.ddl-auto = update spring.datasource.validationQuery = SELECT 1 spring.datasource.url= jdbc:mysql://localhost:3306/sys spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext