我正在尝试使用我的servlet 3.0应用程序内的请求范围。
我没有使用web.xml,而是WebApplicationInitializer的实现。onStartup方法看起来像这样:
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext applicationContext =
new AnnotationConfigWebApplicationContext();
applicationContext.setServletContext(servletContext);
applicationContext.scan("package containing proxy scoped bean and other stuff");
applicationContext.refresh();
servletContext.addListener(new ContextLoaderListener(applicationContext));
servletContext.addListener(new RequestContextListener());
}
和请求作用域bean看起来像这样:
@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class CallerInformation {
private String clientIp;
public String getClientIp() {
return clientIp;
}
public void setClientIp(String clientIp) {
this.clientIp = clientIp;
}
}
现在注入的"CallerInformation"不是cglib代理,但行为类似于原型作用域,它是每个类的不同实例,它不通过请求持有任何信息…
你知道我做错了什么吗?
编辑:我已经尝试了servlet 2.5和web.xml配置相同的场景,它工作得像地狱;)
行为是正确的。实例只保存一个请求。
尝试用@Autowired注释注入spring组件