开发者

Implementing servlet lifecycle methods in a Spring web application?

开发者 https://www.devze.com 2023-02-11 07:27 出处:网络
I am implementing a web application using Spring. I am using Spring\'s ContextLoaderListener, to load up my application contexts, and Spring\'s DispatcherServlet, to load the relevant beans from {name

I am implementing a web application using Spring. I am using Spring's ContextLoaderListener, to load up my application contexts, and Spring's DispatcherServlet, to load the relevant beans from {name}-servlet.xml, which refer to the beans in the main application context. I want to be able to integration test these Spring configurations outside of the container to validate everything is wired up correctly before I deploy to Tomcat. However my applicatio开发者_StackOverflow社区n requires some scheduled background processing when running in the container. In a regular HttpServlet I would simply implement init() and destroy(). All the suggestions I have read suggest using an InitializingBean for that kind of initialization.

However, if I use an InitializingBean, afterPropertiesSet() gets called whether I am inside the container or in integration tests - and outside the container, I don't have access to the resources that background task needs. Is there a better way to perform the tasks I would normally perform in init() and destroy() so that they will only run when deployed as a webapp?


Have you considered using a test spring config file that overrides the bean implementing your background process?

This way everything else in the spring configuration would work normally except for the one overridden bean.

0

精彩评论

暂无评论...
验证码 换一张
取 消