Announcement Announcement Module
Collapse
No announcement yet.
Strange BeanCurrentlyInCreationException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Strange BeanCurrentlyInCreationException

    Hello,

    I use Spring 2.5.2, Hibernate 3.2.6.

    I've put in place two layers in my application, DAO and Service and I use POJO as domain object. So in My example I have:
    Code:
    Public class Company {
    	private String name;
    	private String number;
    
    }
    Code:
    public class CompanyDaoImpl extends HibernateDaoSupport implements CompanyDao{
    void saveCompany(Company company){}
    }
    Code:
    public class CompanyserviceImpl implements companyService {
    	private CompanyDao companyDao = null;
    public void updateCompany(Company company) {
    	
    }
    }
    At this stage all work perfectly, ouf
    After that, Id like to make a history of the change made in the class Company.
    So I decided to use AOP and developed an advice for that; I inject another Dao in my advice to store the data in a history table.

    Code:
    public class HistoryAdvice implements AfterReturningAdvice {
    
    	private HistoryDao historyDao = null;
    	public void afterReturning(Object returnValue, Method method,
    			Object[] args, Object target) throws Throwable {
    
    }
    }
    My application context file :

    Code:
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="configLocation">
    			<value>
    				/com/viaxoft/domain/config/client/hibernate_client.cfg.xml
    			</value>
    		</property>
    	</bean>
    	<bean id="historyDAO" class="com.viaxoft.dao.impl.HistoryDaoImpl">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    	<bean id="companyDAO" class="com.viaxoft.dao.impl.CompanyDaoImpl">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    	<bean id="companyHistoryDAO"
    		class="com.viaxoft.dao.impl.CompanyHistoryDaoImpl">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    	<bean id="historyParametersDAO"
    		class="com.viaxoft.dao.impl.HistoryParametersDaoXMLImpl">
    	</bean>
    	<bean id="pojoStore"
    		class="net.sf.hibernate4gwt.core.store.stateless.StatelessPojoStore" />
    	<bean id="classMapper"
    		class="net.sf.hibernate4gwt.core.beanlib.mapper.DirectoryClassMapper">
    		<property name="rootDomainPackage"
    			value="com.viaxoft.domain.client" />
    		<property name="rootClonePackage" value="com.viaxoft.dto" />
    		<property name="cloneSuffix" value="Dto" />
    	</bean>
    	<bean id="persistenceUtil"
    		class="net.sf.hibernate4gwt.core.hibernate.HibernateUtil">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    	<bean id="beanManager"
    		class="com.viaxoft.ext.beanmanager.BeanManager">
    		<property name="pojoStore" ref="pojoStore" />
    		<property name="persistenceUtil" ref="persistenceUtil" />
    		<property name="classMapper" ref="classMapper" />
    	</bean>
    		<bean id="historyAdvice"
    		class="com.viaxoft.history.HistoryAdvice">
    		<property name="historyDao">
    			<ref bean="historyDAO" />
    		</property>
    		<property name="historyParametersDao">
    			<ref bean="historyParametersDAO" />
    		</property>
    	</bean>
    	<bean id="companyService"
    		class="com.viaxoft.service.impl.CompanyServiceImpl">
    		<property name="companyDao">
    			<ref bean="companyDAO" />
    		</property>
    		<property name="beanManager">
    			<ref bean="beanManager" />
    		</property>
    	</bean>
    
    	<bean id="historyAdvisor"
    		class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    		<property name="advice" ref="historyAdvice" />
    		<property name="patterns" value=".*" />
    	</bean>
    	<bean
    		class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
    </beans>

    And when I run my code in an unit test I have the following Exception :

    org.springframework.beans.factory.BeanCurrentlyInC reationException: Error creating bean with name 'sessionFactory': Bean with name 'sessionFactory' has been injected into other beans [historyDAO] 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.

    I really don't see where I could put a circular reference with the sessionFactory.

  • #2
    Your DefaultAdvisor creates proxies for all your beans (due to your settings in the RegExpMothodPointcutAdvisor). However the advice uses a historyDao, which uses a sessionFactory. The sessionFactory is injected prior to the creation of the proxies, everything is proxied hence the error.

    Also if you want to track changes you are probably better of with a HibernateInterceptor or Listener.

    Comment


    • #3
      I didn't want to use HibernateInterceptor because i consider that the track of the history change is more functional.
      What type of listener do you talk about ?

      Comment


      • #4
        Moreover, I tried to restrict the pattern for the regexp and I put ".*updateCompany".
        But the advice is not run
        So I made a simpler advice that just write to System.out adn it show that the advice is launched after a method called in updateCompany but not after updateCompany itself.
        Last edited by twentyseven; Jun 19th, 2008, 06:05 AM.

        Comment


        • #5
          Well there might be something wrong with your regexp i'm not a regexp guru. Also you are using Spring 2.5 why not simply use an aop config block and use the AspectJ language instead.

          So instead of your RegexpMethodPointcutAdvisor and DefaultAdvisorAutoProxyCreator you would have

          Code:
          <aop:config>
          	<aop:pointcut id="companyServiceMethod" expression="execution(* com.viaxoft.service.companyService+.updateCompany(..))" />
          	<aop:advisor pointcut-ref="companyServiceMethod" advice-ref="historyAdvisor" />
          </aop:config>
          This should only generate a proxy for the CompanyService not the other classes and invoke the history advisor (spring will detect the type because you implement the AfterReturningAdvice interface).

          Comment


          • #6
            I resolved my problems:

            - The first one was effectively due to the fact that my regexp wasn't enough restrictive,
            - The second problem was due to the fact that my unit test didn't used the service correctly.

            Thanks for your help

            Comment

            Working...
            X