【问题标题】:Using Session Scope in Spring Beans在 Spring Beans 中使用 Session 范围
【发布时间】:2011-11-27 17:51:49
【问题描述】:

我将 JSF 2 用于视图,将 Spring 用于业务逻辑。我正在尝试使用注释(@Scope(“session”))将会话范围设置为我的一个 spring bean,但我得到了这个异常:

    SEVERE: Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'handleFiles': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private creazione.util.FTPOperations creazione.components.HandleOldFiles.operations; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'ftpOperations': Scope 'session' 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.

我知道 RequestContextListener。它在我的 web.xml 中。我还添加了 RequestContextFilter:

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

    <filter>
        <filter-name>requestContextFilter</filter-name>
        <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>requestContextFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

似乎没有任何效果。我究竟做错了什么? 谢谢!

【问题讨论】:

    标签: java spring jsf-2


    【解决方案1】:

    如果您使用基于注释的配置,只需在您的主配置 xml 中更改此标记:

    <context:component-scan base-package="my.package"/>
    

    <context:component-scan base-package="my.package" scoped-proxy="targetClass" />
    

    还可以通过注释标记类以使用代理:

    @Component
    @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
    

    这对我有用。 更多细节在这里:4.12 Classpath scanning, managed components and writing configurations using Java

    另外,如果您是通过上下文 xml 配置创建 bean,这里是这种情况的另一个示例:

    <bean id="somebean" class="com.package.beans" scope="session">
        <aop:scoped-proxy/>
    </bean>
    

    【讨论】:

    • 嗨,这对我不起作用。我得到相同错误的所有三种方式都无法连接。
    • 它没有自动装配的原因有很多 - 范围设置损坏,没有 cglib 它的类路径(如果你在没有接口的情况下自动装配)。有关详细信息,请参阅您的错误消息。
    【解决方案2】:

    尝试使用 aop:scoped-proxy 将必须注入的 bean 定义为会话。

    <bean id="ftpOperations" class="..." scope="session">
        <aop:scoped-proxy/>
    </bean>
    

    如果还没有相关的命名空间,请添加:

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
        ...
    xsi:schemaLocation="
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            ...
    

    【讨论】:

    • 是的,但我正在使用注释。我在 applicationContext.xml 中有相当多的 spring bean 要编写。是否有与 等效的注释?
    • 还是不行...我已经添加了@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES),我已经为每个spring bean重构了一个接口,但是现在AutoWiring 不再识别我的 bean...
    • 错误信息是什么?请记住,您可以指定一个限定符来帮助连接过程,例如:@Autowired @Qualifier("beanName")
    • 是的,我使用了限定词;例外情况是: 原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为 [creazione.util.FTPOperations] 的匹配 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=ftpOperations)}
    猜你喜欢
    • 1970-01-01
    • 2017-07-08
    • 2011-11-03
    • 2017-05-13
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多