我在我的dao类中注入一个hibernate sessionfactory bean时遇到问题。说没有豆牌有这个名字
@Repository("emaildao")
public class EmailDAOImpl implements EmailDAO{
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory s){
sessionFactory = s;
}
@Override
public void saveMessages(List<Message> messages) {
// some code persisting messages
}
这是我的配置文件
@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.opm.emailheader" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "com.opm.emailheader"} );
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
properties.put("hibernate.query_cache", environment.getRequiredProperty("hibernate.query_cache"));
properties.put("hibernate.query_cache_size", environment.getRequiredProperty("hibernate.query_cache_size"));
properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibarnate.hbm2ddl.auto"));
properties.put("hibernate.connection.CharSet","utf8");
properties.put("hibernate.connection.characterEncoding","utf8");
properties.put("hibernate.connection.useUnicode",true);
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
代码结构文件夹如下: com.opm.emailheader
应用
道
daoimpl
CONF
HibernateConfiguration.java AppInitializer.java WebConfig.java SecurityConfig.java
异常堆栈跟踪: 线程“main”中的异常org.springframework.beans.factory.BeanCreationException:创建名为'emaildao'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配方法:public void com.opm.emailheader.app.dao.daoimpl.EmailDAOImpl.setSessionFactory(org.hibernate.SessionFactory);嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型[org.hibernate.SessionFactory]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{} project structure