Announcement Announcement Module
Collapse
No announcement yet.
Interesting Situation : DI in Advice Class results in Circular Reference Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Interesting Situation : DI in Advice Class results in Circular Reference

    Here is the situation.
    I have a Advice class named "AddressBlockPrivilegeAdvice.java" which implements MethodInterceptor and ofcourse has an implementation for invoke() method.

    I am using Helper class in Advice named "PrivilegeValidator" to checkPrivileges(). In order to check privileges i need to do Dependency Injection let say i am injecting bean named LoginManager.

    Please note as the Advice would eventually will be applied to Proxy whose implementation class also has dependency injection toLoginManager. I guess this is the reason it results in Circular Reference. Wondering if Spring version2.5.3 provides a way to resolve Circular reference or i need to take another approach.

    Here is the snippet
    Spring context xml file
    Code:
    <!-- Advice class -->
       <bean id="addressBlockPrivilegeAdvice"
          class="com.XXX.aspects.AddressBlockPrivilegeAdvice"> 
              <property name="privilegeValidator" ref="privilegeValidator"></property>     
       </bean>
    
    <bean id="privilegeValidator" class="com.XXX.service.validation.PrivilegeValidator">
          <property name="manageAddrBlkManager" ref="manageAddrBlkManager" /> 
          <property name="loginManager" ref="loginManager" />
        </bean>
    Code:
    public class AddressBlockPrivilegeAdvice implements MethodInterceptor
    {
            private PrivilegeValidator privilegeValidator ;
    ---
    ---
    ---
    }
    Code:
    public class PrivilegeValidator 
    {
        private ManageAddrBlkManager manageAddrBlkManager;
        private LoginManager loginManager;
    ---
    ---
    ---
    }
    Code:
    Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'loginManager': Bean with name 'loginManager' has been injected into other beans [privilegeValidator] in its raw version as part of a circular reference, but has eventually been wrapped. 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.doCreateBean(AbstractAutowireCapableBeanFactory.java:498)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:217)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
    	... 61 more

  • #2
    I hope someone answer my posting. In continuation to earlier posting i am thinking the other reason for Circular reference might be the LoginManager itself is Proxy because we have the following configuration
    Code:
    <bean
            id="hbmTransactionProxy"
            class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
            <property name="beanNames">
                <list>
                    <value>*Manager</value>
                </list>
            </property>
            <property name="interceptorNames">
                <list>                 
                  <idref bean="addressBlockPrivilegeAdvisor" />
                  <idref bean="txInterceptor" />
                </list>
            </property>
        </bean>
    As you see all the bean names ending with *Manager will be proxy hence LoginManager is Proxy . Taking that into consideration i created a copy of LoginManager but with id value as LoginService and it works fine. But i think this is ugly solution as i need to define LoginManager twice with different Ids.
    Here is the snippet
    Code:
    <bean id="loginManager" class="com.boeing.nmt.nams.service.impl.LoginManagerImpl">
            <property name="namsUserDao" ref="namsUserDao" />
            <property name="userRoleDao" ref="userRoleDao" />
        </bean>
        
        <bean id="loginService" class="com.boeing.nmt.nams.service.impl.LoginManagerImpl">
            <property name="namsUserDao" ref="namsUserDao" />
            <property name="userRoleDao" ref="userRoleDao" />
        </bean>

    Comment

    Working...
    X