Spring可以轻松集成到任何基于Java的Web框架中。你需要做的就是在web.xml
中声明ContextLoaderListener
并使用contextConfigLocation
设置要加载的上下文文件。
<context-param>:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
然后,你可以使用WebApplicationContext来获取bean的句柄。
WebApplicationContext ctx = WebApplicationContextUtils.getrequiredWebApplicationContext(servlet.getServletContext());
SomeBean someBean = (SomeBean) ctx.getBean("someBean");