开发者社区 问答 正文

在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>

展开
收起
a123456678 2016-03-17 15:16:21 2893 分享 版权
1 条回答
写回答
取消 提交回答
  • 可能你没有把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>
    2019-07-17 19:05:29
    赞同 展开评论
问答分类:
问答标签:
问答地址: