在Spring Beans中使用会话范围

spa*_*uny 8 java spring jsf-2

我使用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.
Run Code Online (Sandbox Code Playgroud)

我知道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>
Run Code Online (Sandbox Code Playgroud)

似乎没什么用.我究竟做错了什么?谢谢!

msa*_*gel 27

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

<context:component-scan base-package="my.package"/>
Run Code Online (Sandbox Code Playgroud)

<context:component-scan base-package="my.package" scoped-proxy="targetClass" />
Run Code Online (Sandbox Code Playgroud)

此外,可以通过注释标记用于处理代理的类:

@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
Run Code Online (Sandbox Code Playgroud)

这对我有用.更多细节:4.12类路径扫描,托管组件和使用Java编写配置

另外,如果你是通过上下文xml配置创建bean,这是另一个例子:

<bean id="somebean" class="com.package.beans" scope="session">
    <aop:scoped-proxy/>
</bean>
Run Code Online (Sandbox Code Playgroud)


sti*_*vlo 10

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

<bean id="ftpOperations" class="..." scope="session">
    <aop:scoped-proxy/>
</bean>
Run Code Online (Sandbox Code Playgroud)

如果相关名称空间不存在,则还要添加它:

<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
        ...
Run Code Online (Sandbox Code Playgroud)

  • 是的,但我正在使用注释.我在applicationContext.xml中编写了很多spring bean.是否有与<aop:scoped-proxy />等效的注释? (3认同)