在spring中获得request对象
各位有一个问题,我想在spring中继承json-rpc然后自定义了bean在启动的时候加载并注册.可是按照网上 http://mazhihui.iteye.com/blog/1662897 的方法总是说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.当我设置bean的scope之后有无法运行.请问下怎么办.以下是代码:
public class Register{ @Autowired private HttpServletRequest request; private String packages[]; public void init(){ for(String pack:packages){ Set<Class<?>> set = ClassUtil.getClasses(pack); for(Class<?> clazz:set){ Annotation[] annotations = clazz.getAnnotations(); for(int i=0 ;i<annotations.length;i++){ if(annotations[i].annotationType().getName().equals(RPC.class.getName())){ RPC rpc = clazz.getAnnotation(RPC.class) ; String name = rpc.name() ; if("".equals(name)){ name = ClassUtil.getClassName(clazz) ; } JsonRpcRegister.registerObject(request, name, clazz) ; /*JSONRPCBridge bridge = new JSONRPCBridge(); bridge.registerObject(name, clazz) ;*/ } } } } } public String[] getPackages() { return packages; } public void setPackages(String[] packages) { this.packages = packages; } }
<bean class="com.test.rpc.Register" init-method="init" scope="prototype"> <property name="packages"> <list> <value>com.fiz.enap</value> </list> </property> </bean>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能你没有把request绑定的提供服务的线程上,要再web.xml中配置以后才可以在Spring中使用,
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener>
</listener>
或者
<filter>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
<filter-name>requestContextListener</filter-name>
</filter>
<filter-mapping>
<filter-name>requestContextListener</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
具体你可以看看这篇文章 :http://mazhihui.iteye.com/blog/1662897
request 是http调用时候才有的哦,所以spring mvc 一般都是作为方法的参数传递.
像你这样作为系统启动,都没有请求哪里来的request