2014-03-27 113 views
0

我在Spring中有一個計劃任務,每24小時執行一次,我也可以在任何時候作爲異步任務觸發。發生火災時,我會得到以下例外情況。彈簧循環引用注入問題

org.exampletest.toa.data.error.DataAccessException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.interceptor': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. 
at org.exampletest.toa.data.pool.implementations.UserService.getObjectFromOriginalDataSource(UserService.java:60) ~[helpsteps-core-1.0.2-SNAPSHOT.jar:na] 
at org.exampletest.toa.data.pool.implementations.UserService.getObjectFromOriginalDataSource(UserService.java:28) ~[helpsteps-core-1.0.2-SNAPSHOT.jar:na] 
at org.exampletest.toa.data.pool.ObjectPool.getObjectFromDataSource(ObjectPool.java:241) ~[helpsteps-core-1.0.2-SNAPSHOT.jar:na] 
at org.exampletest.toa.data.pool.ObjectPool.getCopy(ObjectPool.java:133) ~[helpsteps-core-1.0.2-SNAPSHOT.jar:na] 
at org.exampletest.toa.data.pool.implementations.UserService.getCopy(UserService.java:82) ~[helpsteps-core-1.0.2-SNAPSHOT.jar:na] 
at org.exampletest.toa.messaging.dispatch.MessageDispatcher.dispatch(MessageDispatcher.java:162) ~[helpsteps-core-1.0.2-SNAPSHOT.jar:na] 
at org.exampletest.toa.messaging.dispatch.MessageDispatcher.dispatchNightly(MessageDispatcher.java:82) ~[helpsteps-core-1.0.2-SNAPSHOT.jar:na] 
... 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.interceptor': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:342) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE] 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE] 
at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:33) ~[spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE] 
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.getTarget(Cglib2AopProxy.java:654) ~[spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE] 
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:605) ~[spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE] 
at org.exampletest.toa.access.PrivilegedMethodInterceptor$$EnhancerByCGLIB$$6ed9064e.invoke(<generated>) ~[cglib-2.2.2.jar:na] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE] 
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) ~[spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE] 
at com.sun.proxy.$Proxy190.getGroupSubscribers(Unknown Source) ~[na:na] 
at org.exampletest.toa.data.implementations.mysql.MySQLUserDAO.fillInGroupDetails(MySQLUserDAO.java:8406) ~[helpsteps-core-1.0.2-SNAPSHOT.jar:na] 
at org.exampletest.toa.data.implementations.mysql.MySQLUserDAO.setUserRelationships(MySQLUserDAO.java:2698) ~[helpsteps-core-1.0.2-SNAPSHOT.jar:na] 
at org.exampletest.toa.data.implementations.mysql.MySQLUserDAO.getUser(MySQLUserDAO.java:5314) ~[helpsteps-core-1.0.2-SNAPSHOT.jar:na] 

這裏是我的applicationContext.xml

<bean id="messageDispatcher" class="org.exampletest.toa.messaging.dispatch.MessageDispatcher" scope="singleton"> 
    <property name="userDAO" ref="userDAO_raw"/> 
    <property name="userService" ref="userPool_raw"/> 
</bean> 

<bean id="groupDAO_raw" class="org.exampletest.toa.data.implementations.mysql.MySQLGroupDAO" scope="singleton"> 
    <property name="dataSources" ref="dataSources"/> 
    <property name="userDAO" ref="userDAO_raw" /> 
    ... 

<bean id="userDAO_raw" class="org.exampletest.toa.data.implementations.mysql.MySQLUserDAO" scope="singleton"> 
    <property name="dataSources" ref="dataSources"/> 
    <property name="groupDAO" ref="groupDAO_raw" /> 
    ... 
</bean> 

<bean id="userPool_raw" class="org.exampletest.toa.data.pool.implementations.UserService" scope="singleton"> 
    <property name="dao" ref="userDAO_raw" /> 
    ... 
</bean> 

<bean id="userDAO" class="org.springframework.aop.framework.ProxyFactoryBean"> 

    <property name="target"> 
     <ref bean="userDAO_raw"/> 
    </property> 
    <property name="interceptorNames"> 
     <list> 
      <value>interceptor</value> 
     </list> 
    </property> 
</bean> 

<bean id="groupDAO" class="org.springframework.aop.framework.ProxyFactoryBean"> 
    <property name="target"> 
     <ref bean="groupDAO_raw"/> 
    </property> 
    <property name="interceptorNames"> 
     <list> 
      <value>interceptor</value> 
     </list> 
    </property> 
</bean> 


<bean id="userPool" class="org.springframework.aop.framework.ProxyFactoryBean"> 
    <property name="target"> 
     <ref bean="userPool_raw"/> 
    </property> 
    <property name="interceptorNames"> 
     <list> 
      <value>interceptor</value> 
     </list> 
    </property> 
</bean> 

<bean id="interceptor" class="org.exampletest.toa.access.PrivilegedMethodInterceptor" scope="request"> 
    <aop:scoped-proxy/> 
    <property name="environment" ref="environment" /> 
    <property name="session" ref="toasession" /> 
    <property name="methodContextManager" ref="methodContextManager" /> 
    <property name="userDAO" ref="userDAO_raw" /> 
    ... 
</bean> 

我很困惑。我正在將'raw'userDAO和userPool注入到我的任務類(MessageDispatcher)中,並將'raw'groupDAO注入到userDAO中,反之亦然,但異常似乎表明Spring仍在創建代理對象groupDAO。循環參考注射有問題嗎? (我認爲Spring自動處理。)我不知道我做錯了什麼。

回答

0

所以我想出了問題。實際上,我們有幾個應用程序上下文可以導入到基礎applicationContext.xml中,具體取決於我們的部署。在其中一個導入的應用程序上下文中,我重寫了默認的'userDAO'bean,並且未能將'groupDAO_raw'注入到該重寫的'userDAO'bean中。因此,如果不這樣做,必須在其中注入代理引用,導致出現問題。一旦我將原始bean注入到頂級的userDAO bean中,生活就會變得更好。