javaconfig配置的sessionFactory在DAO层怎么注入进来
@Configuration
public class AppConfig {
@Bean
public SessionFactory sessionFactory() throws Exception{
AnnotationSessionFactoryBean factory =new AnnotationSessionFactoryBean();
factory.setDataSource(dataSource());//设置数据据
factory.setPackagesToScan(new String[]{"algz.platform"});
factory.afterPropertiesSet();
return factory.getObject();
}
DAO:
@Repository("SQLiteDao")
public class SQLiteDaoImpl implements SQLiteDao {
@Autowired
private SessionFactory sessionFactory;
启动报错:
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), @org.springframework.beans.factory.annotation.Qualifier(value=sessionFactory)}
请问怎样在DAO层注入进来。
javaconfig spring3 annotations autowired --------------------编程问答-------------------- 注解注入的话 。 给你的Dao 类头前面加 @service =XXDAO , 在你要注入的地方添加 @@Repository("XXDAO")
思路是这样,具体语法找百度 --------------------编程问答--------------------
DAO类上应为@Repository;service类上@service。这与我的问题不相关。我的其他注入都正常,就是sessionFactory注入进来。因为是javaConfig写法。
补充:Java , Web 开发