配置类名称以黄色标记,显示 Application context not configured for this file
block 引用>My Config Class :
@Configuration @EnableTransactionManagement public class DatabaseConfig { @Bean public LocalSessionFactoryBean sessionFactory() { LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); sessionFactory.setPackagesToScan("com.tornikeshelia.model"); sessionFactory.setHibernateProperties(getHibernateProperties()); return sessionFactory; } private Properties getHibernateProperties(){ Properties prop = new Properties(); Properties properties = new Properties(); properties.setProperty("hibernate.hbm2ddl.auto", "update"); properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL57InnoDBDialect"); return properties; } @Bean(name = "dataSource") public BasicDataSource dataSource(){ BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName("com.mysql.cj.jdbc.Driver"); ds.setUrl("jdbc:mysql://localhost:3306/test?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=utf-8"); ds.setUsername("username"); //ds.setPassword(""); return ds; } @Bean @Autowired public HibernateTransactionManager transactionManager(SessionFactory sessionFactory){ HibernateTransactionManager txManager = new HibernateTransactionManager(); txManager.setSessionFactory(sessionFactory); return txManager; } @Bean public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){ return new PersistenceExceptionTranslationPostProcessor(); } }
我已经注释掉了
ds.setPassword
因为我的测试数据库没有任何密码保护。另外, hibernate 不会从我的Entity
自动创建表class ,我认为那是因为 Spring 没有读取这个配置文件当尝试
autowire
时我的 SessionFactorDaoImpl
类通过:@Resource(name = "sessionFactory") private SessionFactory session;
错误显示
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.tornikeshelia.dao.PersonDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
block 引用>SOLUTION : The problem was 1)The location of config package and 2) in the warning message that was on the class name (yellowed out with message -
Application context not configured for this file
So if anybody has same problem here's the solution -1) move your config package inside the parent package where the model package is
2) Step on the class name (click anywhere on the class name) the yellow light bulb will appear, click on it and then click on
configure application context
, a new tab will appear where you should choose this class and pressokay
.
最佳答案
尝试编辑您的配置类并更新 transationManager() bean:
@Bean
public HibernateTransactionManager transactionManager(){
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
您可能会发生错误,因为您尝试注入(inject)尚未创建的 bean。
关于java - 无法使用 Java 配置 Autowiring SessionFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57108860/