0
我尝试动态初始化我的Hibernate DAO实例。在运行时动态创建支持autowire的Spring bean
什么给出:
- 通用DAO(
GenericDaoImpl<T,PK extends Serializable>
) - DAO厂,应在包中每个模型类创建一个通用的DAO实例(我尝试与反思的东西)
- 豆类似乎要创建,但只要我想自动装配我收到一个异常
- 春“3.2.4.RELEASE”环境
GenericDaoFactory
@Configurable
public class GenericDaoFactory {
@Autowired private AutowireCapableBeanFactory beanFactory;
@Autowired private SessionFactory sessionFactory;
@PostConstruct
private void createDynamicDaoBean() {
try {
// Example for employee variant
GenericDaoImpl<Employee, Integer> employeeDao = new GenericDaoImpl<Employee, Integer>(Employee.class, sessionFactory);
beanFactory.autowireBean(employeeDao);
beanFactory.initializeBean(employeeDao, "employeeDao");
} catch(Exception e) {
e.getMessage();
}
}
}
异常
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com..test.dao.GenericDaoImpl com.test.service.EmployeeService.employeeDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
为什么?为什么重新发明Spring Data JPA已经发明的轮子? –