奇怪的春天"无法解决的循环参考问题"

时间:2015-10-14 04:46:44

标签: java spring hibernate annotations

在我的应用程序中,我们使用多个数据源,因此我们有多个数据库配置(会话工厂)。一切都在我们的本地(whindows机器)上运行得很好但是当我们将war文件部署到Unix应用程序时失败并出现以下异常:

  

org.springframework.beans.factory.BeanCreationException:错误   创建名称为' cpnRepository':注入自动装配的bean   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:private org.hibernate.SessionFactory   org.npcc.ccms.dao.GenericDB1Dao.sessionFactory;嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名称为' DB1Config':注入自动装配的bean   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:private org.hibernate.SessionFactory   org.npcc.ccms.config.db.DB1Config.sessionFactory;嵌套异常是   org.springframework.beans.factory.BeanCurrentlyInCreationException:   创建名为' DB1SessionFactory':请求bean的bean时出错   目前正在创作:是否存在无法解决的循环引用?

这是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依赖关系?为什么两个操作系统之间的顺序不一致?在此先感谢。

1 个答案:

答案 0 :(得分:2)

我认为您的问题来自于您正在创建LocalSessionFactoryBean并同时尝试自动加载SessionFactory ......

您的sessionFactory课程中不需要该成员DB1Config,请尝试类似的内容:

@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();
}