在Singleton bean中请求scoped bean

时间:2013-09-14 05:41:12

标签: spring scope request javabeans

我有一个Web应用程序,我定义了2个bean 1具有单例范围,1具有请求范围。 我需要在singleton bean中使用请求范围的bean。

所以我最终得到了这个

<bean id="routingDataSource" class="org.ai2l.ifsr.service.multiTenants.RoutingDataSource" scope="request">
    <aop:scoped-proxy/>
    <property name="targetDataSources" value="#{tenantSpecificDependencyFactory.tenantIdToDataSource}"/>
    <!-- <property name="defaultTargetDataSource" ref="parentDataSource"/>         -->
</bean>


<bean id="entityManagerFactory"   class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="ifsrPU"/>
    <property name="dataSource" ref="routingDataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" value="${hibernate.dialect}"/>
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.ejb.naming_strategy">
              org.hibernate.cfg.ImprovedNamingStrategy
            </prop>
        </props>
    </property>

</bean>

正如您将注意到我使用<aop:scoped-proxy/>但它无效。 我也试过了

  <listener>
<listener-class>
  org.springframework.web.context.request.RequestContextListener
</listener-class>

在我的web.xml中。但这一切都没有效果。

我仍然收到此错误

Caused by: 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.

有人知道为什么会这样吗?

0 个答案:

没有答案