Servlets 在Spring MVC中未找到线程绑定请求
我正在使用SpringMVC构建一个应用程序,并试图从DispatcherServlet范围之外的bean访问http请求。我读了类似的帖子,发现我需要添加RequestContextListener。但是,我一直收到相同的错误。下面您可以看到控制器和我注入控制器的bean:Servlets 在Spring MVC中未找到线程绑定请求,servlets,spring-mvc,Servlets,Spring Mvc,我正在使用SpringMVC构建一个应用程序,并试图从DispatcherServlet范围之外的bean访问http请求。我读了类似的帖子,发现我需要添加RequestContextListener。但是,我一直收到相同的错误。下面您可以看到控制器和我注入控制器的bean: @Controller public class ChargeController { // Injected bean @Autowired public StripeCharger strip
@Controller
public class ChargeController {
// Injected bean
@Autowired
public StripeCharger stripeCharger;
@RequestMapping(value = "/stripecharge", method = RequestMethod.POST)
public String chargeStripe(ModelMap model) {
// user`s custom code
String response = stripeCharger.charge();
}
}
web.xml的一部分
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<context:component-scan base-package="com.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- This bean is auto-generated -->
<!-- define the bean to inject the Stripe charger -->
<bean id="StripeCharger" class="com.controller.StripeCharger" scope="request">
<aop:scoped-proxy/>
</bean>
</beans>
有人能帮我解决这个问题吗?谢谢你也许这会有所帮助:谢谢你的评论。我认为问题在于我在bean的构造函数中使用了请求。但在初始化构造函数时,没有请求。我需要将它移动到bean的方法中,一旦有一个传入的请求,这个bean就会被调用。是的……我在考虑这个问题,但是因为我没有这样编码,我决定在互联网上寻找更合理的答案。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<context:component-scan base-package="com.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- This bean is auto-generated -->
<!-- define the bean to inject the Stripe charger -->
<bean id="StripeCharger" class="com.controller.StripeCharger" scope="request">
<aop:scoped-proxy/>
</bean>
</beans>