既可以使用 web.xml 来使 Web 容器加载 Spring,也可以通过 struts-config.xml 来使 Web 容器加载 Spring。
1、使用 web.xml 来使 Web 容器加载 Spring
web.xml :
通过:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener-->
或:
<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Web 容器会自动加载 /WEB-INF/applicationContext.xml 初始化 ApplicationContex t实例;
也可以通过
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml</param-value>
</context-param>
使 Web 容器加载指定名称路径的 Spring 配置文件。
2、通过 struts-config.xml 来使 Web 容器加载 Spring
struts-config.xml
通过
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml,
/WEB-INF/action-servlet.xml"/>
</plug-in>
来加载 Spring 配置文件。
本文介绍了两种让Web容器加载Spring框架的方法:一是通过web.xml配置文件中的ContextLoaderListener或ContextLoaderServlet;二是利用struts-config.xml中的ContextLoaderPlugIn插件来指定Spring配置文件的位置。

被折叠的 条评论
为什么被折叠?



