我在编写人员与用户管理模块代码时遇到一些问题,用户与人员是一对一关联,我想在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
请高手帮忙解决一下!
<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
请高手帮忙解决一下!
解决方案 »
- 高手指教
- null id in cn.csdn.domain.UserBook entry (don't flush the Session after an excep
- tomcat 5.5安装
- JAVA是怎样接收SOAP协议的包头的?
- Hibernate取Session需要用单例吗?
- jbpm:我把jbpm发布到jboss中,在ejb中调用jbpm,当进入node时触发action..
- 求通过jdbc从sybase中取出数据然后插入oracle中的代码
- struts高手请进!!!!!
- 谁研究过Struts自带的Example?他在显示Current Subscriptions是怎么做的?
- 小女子求帮助IDEA setting 中修改 file types 后
- mvc模式的简单登录求助
- 怎样才能dtd文件放到myeclipse创建的项目中
当添加一个用户时,需要指定对应的人员标识,人员不存在不允许添加,所以在UserDao中持有一个PersonDao。
请问在spring能否将我的需求改成如下形式:
PersonDao personDao = new PersonDao();
UserDao userDao = new UserDao();
personDao.setUserDao(userDao);
userDao.setPersonDao(personDao);添加一个Dao的话有点麻烦
当添加一个用户时,需要指定对应的人员标识,人员不存在不允许添加为什么非要在DAO层处理呢?
耦合度不就高了吗?
如果用hibernate的话也可以在hibernate的映射文件里设置ondelete=cascade
如果是我的话,我会做成personDAO只操作person表,userDAO只操作user表
user对象,判断user是否为空,再删除用户,那么像这种判断为空要不要放到Dao层来做呢?
user对象,判断user是否为空,再删除用户,那么像这种判断为空要不要放到Dao层来做呢?
判断是否为空不用放到DAO层吧,查出来user==null不就行了吗,只是删除的时候一个条件而已嘛
为什么要调用personDao的方法?addUser方法只要往user表里插一条数据就可以了,至于有没有这个员工在业务层判断,在业务层确定有这个员工了,才调用addUser(User user, int personId)不是吗?
this.getHibernateTemplete().save(),
this.getHibernateTemplete().update(),
this.getHibernateTemplete().get(),
this.getHibernateTemplete().delete()...
我建议让PersonDao单方面持有UserDao,因为这里有一个关联删除的逻辑,让Person引用User便于操作,
而UserDao中持有一个PersonID我觉得就可以了。