我在编写人员与用户管理模块代码时遇到一些问题,用户与人员是一对一关联,我想在personDao中持有UserDao,在UserDao中持有personDao,spring的配置如下(版本spring 2.0):
<bean id="personDao" class="cn.edu.ytu.oa.dao.impl.PersonDao4HibernateImpl" lazy-init="true">
<property name="sessionFactory" ref="sessionFactory" />
<property name="orgDao" ref="orgDao" />
<property name="userDao" ref="userDao" />
</bean>
<bean id="userDao" class="cn.edu.ytu.oa.dao.impl.UserDao4HibernateImpl" lazy-init="true">
<property name="sessionFactory" ref="sessionFactory" />
<property name="personDao" ref="personDao" />
</bean>
<bean id="orgDao" class="cn.edu.ytu.oa.dao.impl.OrgDao4HibernateImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
在做人员模块的单元测试的添加操作时,出现循环依赖的问题。
添加操做的代码:
private static BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext-*.xml");
public void testAdd() {
PersonDao personDao = (PersonDao) beanFactory.getBean("personDao");
// Person person = new Person();
// person.setName("11111");
// personDao.addPerson(person, 1);
}
异常信息如下:
java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestLoader.createTest(JUnit3TestLoader.java:78)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestLoader.getTest(JUnit3TestLoader.java:95)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestLoader.loadTests(JUnit3TestLoader.java:59)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:445)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personMgr' defined in file [D:\My document\eclipse\workspace\OA\oa_02\WebRoot\WEB-INF\classes\applicationContext-beans.xml]: Cannot resolve reference to bean 'personDao' while setting bean property 'personDao'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'personDao': Bean with name 'personDao' has been injected into other beans [userDao] in its raw version as part of a circular reference, but has eventually been wrapped (for example as part of auto-proxy creation). This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:254)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:128)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:955)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:729)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:416)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:290)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:92)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:77)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:68)
at cn.edu.ytu.oa.PersonDaoTest.<clinit>(PersonDaoTest.java:16)
... 11 more
Caused by:
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'personDao': Bean with name 'personDao' has been injected into other beans [userDao] in its raw version as part of a circular reference, but has eventually been wrapped (for example as part of auto-proxy creation). This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:423)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:246)
... 25 more
请高手帮忙解决一下!