我将 JSF 2 用于 View ,将 Spring 用于业务逻辑。我正在尝试使用注释 (@Scope("session")) 将 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>
似乎没有任何效果。我究竟做错了什么? 谢谢!
最佳答案
如果您使用基于注解的配置,只需在您的主配置 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>
关于java - 在 Spring Bean 中使用 session 作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7598412/