Spring Circular Reference注入问题

时间:2014-03-27 13:33:52

标签: java spring

我在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'groupDA注入userDAO,反之亦然,但异常似乎表明Spring仍在创建一个代理对象groupDAO。圆形参考注射有问题吗? (我以为Spring会自动处理。)我不确定我做错了什么。

1 个答案:

答案 0 :(得分:0)

所以我想出了问题所在。实际上,我们有几个应用程序上下文可以导入到基本的applicationContext.xml中,具体取决于我们的部署。在其中一个导入的应用程序上下文中,我覆盖了默认的&#39; userDAO&#39; bean和我没有注入&#39; groupDAO_raw&#39;把豆变成最重要的用户DAO&#39;豆。因此,未能做到这一点必须在那里注入代理引用,导致问题。一旦我将原始bean注入到重写的userDAO bean中,生活就会好转。