我在尝试使用会话范围的bean时遇到错误,错误是:
HTTP Status 500 - Error creating bean with name 'scopedTarget.usuarioAutenticado': 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.
我的配置是: web.xml中:
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/applicationContext*.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/sys/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
豆子:
<bean id="usuarioAutenticado" class="com.wi.security.UsuarioAutenticado" scope="session">
<aop:scoped-proxy/>
</bean>
我一直在搜索这个问题,我找到的就是将以下行添加到web.xml:
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
这解决了这个问题,但副作用是我的所有@Scheduled带注释的方法都运行了两次,我认为这是因为spring正在创建两个上下文。
阅读Spring文档,它说:
DispatcherServlet,RequestContextListener和RequestContextFilter都完全正确 同样的事情,即将HTTP请求对象绑定到为该请求提供服务的Thread。这个 使请求和会话范围的bean在调用链中进一步可用。
所以,我不明白为什么不使用DispatcherServlet配置。 希望有人能帮助我找到解决方案,我是新手。 提前谢谢。
答案 0 :(得分:0)
知道了!
我在web.xml中设置了contextConfigLocation:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext*.xml</param-value>
</context-param>
然后在servlet配置中再次设置它:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
解决方案是删除servlet param-value:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value/>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>