配置类名称标记为黄色,并显示Application context not configured for this file
我的配置类:
@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
注释掉,因为我的测试数据库没有任何密码保护。另外,休眠状态不会根据我的Entity
类自动创建表,我认为那是因为Spring无法读取此配置文件
通过以下方式尝试在我的autowire
类中使用DaoImpl
SessionFactor时:
@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)}
解决方案: 问题是1)配置软件包的位置和2)在类名上的警告消息中(用消息-
Application context not configured for this file
泛黄) 因此,如果有人遇到相同的问题,这就是解决方案-1)将配置包移到模型包所在的父包中
2)踩上班级名称(在班级名称上单击任意位置),将出现黄色灯泡,单击它,然后单击
configure application context
,将出现一个新标签,您可以在其中选择该班级,然后按okay
。
答案 0 :(得分:0)
尝试编辑您的配置类并更新transationManager()bean:
@Bean
public HibernateTransactionManager transactionManager(){
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
您可能会发生错误,因为您尝试注入尚未创建bean的bean。