Initialize Singletons in Spring Framework 3 MVC

流过昼夜 提交于 2019-12-12 07:02:28

问题


I am writing Spring 3.1.0 MVC based application. The problem is: i want to put some objects in a singleton object (current HttpServletRequest and HttpSevletResponse) to use them in other objects(Spring Controllers). But couldn't do so. I tried to extend DispatcherServlet, overriding both doService and doDispatch. Also tried to implement own HandlerInterceptor. No result.

Where can I initialize my singleton objects? And where is Spring Frameworks's entry point and destroy point (i.e. like init() and destroy() methods or lifecycle)?


回答1:


The current HttpServletRequest and HttpServletResponse are available as method arguments to your controller methods:

@RequestMapping("/foo")
public String foo(HttpServletRequest request) {

}

I believe you can also @Inject them in your controller. A proxy will be injected, and each time you refer to them the current ones will be used. (I'm not 100% certain about this one)

A third option is to use the RequestContextHolder container, and get everything from there.



来源:https://stackoverflow.com/questions/8825177/initialize-singletons-in-spring-framework-3-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!