在我的应用程序中,我们使用多个数据源,因此我们有多个数据库配置(会话工厂).一切都在我们的本地(whindows机器)上运行得很好,但是当我们将war文件部署到Unix应用程序时失败并出现以下异常:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name ‘cpnRepository’: Injection of autowired
dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private org.hibernate.SessionFactory
org.npcc.ccms.dao.GenericDB1Dao.sessionFactory; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name ‘DB1Config’: Injection of autowired
dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private org.hibernate.SessionFactory
org.npcc.ccms.config.db.DB1Config.sessionFactory; nested exception is
org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name ‘DB1SessionFactory’: Requested bean is
currently in creation: Is there an unresolvable circular reference?
这是dao实现:
@Repository("cpnRepository")
public class ProgramNodeDaoImpl extends GenericDB1Dao<Integer, CustomProgramNode> implements CPNRepositoryDao {
@Override
public List<CustomProgramNode> findAllNodes() {
Criteria criteria = createEntityCriteria();
return (List<CustomProgramNode>) criteria.list();
}
@Override
public List<CustomProgramNode> findByStatus(String status) {
Query query = getSession().createQuery("from CustomProgramNode where status = :status");
query.setParameter("status", status);
List<CustomProgramNode> list = query.list();
return list;
}
}
DB1的通用DAO:
public abstract class GenericDB1Dao<PK extends Serializable, T> {
private final Class<T> persistentClass;
@SuppressWarnings("unchecked")
public GenericAgrgtrDao(){
this.persistentClass =(Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
}
@Autowired
@Qualifier("DB1SessionFactory")
private SessionFactory sessionFactory;
protected Session getSession(){
return sessionFactory.getCurrentSession();
}
@SuppressWarnings("unchecked")
public T getByKey(PK key) {
return (T) getSession().get(persistentClass, key);
}
public void persist(T entity) {
getSession().persist(entity);
}
public void delete(T entity) {
getSession().delete(entity);
}
protected Criteria createEntityCriteria(){
return getSession().createCriteria(persistentClass);
}
}
Database1配置:
@Configuration
public class DB1Config {
final static Logger logger = LogManager.getLogger(DB1Config.class);
@Autowired
private Environment environment;
@Autowired
@Qualifier("DB1SessionFactory")
private SessionFactory sessionFactory;
@Bean(name="DB1SessionFactory")
public LocalSessionFactoryBean db1SessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "org.npcc.ccms.model.db1" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean(destroyMethod="")
public DataSource dataSource() {
JndiTemplate jndi = new JndiTemplate();
DataSource dataSource = null;
try {
dataSource = (DataSource) jndi.lookup(environment.getRequiredProperty("datasource.db1"));
} catch (NamingException e) {
}
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"));
return properties;
}
@Primary
@Bean(name="DB1TransactionManager")
public HibernateTransactionManager db1TransactionManager() {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(this.sessionFactory);
txManager.setDataSource(dataSource());
return txManager;
}
}
Spring如何解析bean依赖?为什么两个操作系统之间的顺序不一致?提前致谢.
最佳答案 我认为您的问题来自于您正在创建LocalSessionFactoryBean并同时尝试自动装配SessionFactory …
您在DB1Config类中不需要该类成员sessionFactory,尝试类似的方法:
@Bean(name="DB1SessionFactory")
public LocalSessionFactoryBean db1SessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "org.npcc.ccms.model.db1" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public SessionFactory sessionFactory() {
return db1SessionFactory().getObject();
}