SRPING容器自动加载STRUTS应用
一、MVC框架创建ApplicationContext实例,在MVC框架加载时自动创建SPRING容器。
1、由spring容器管理action两种方式:
通过contextConfigLocation属性载入,多个配置文件则用","隔开。
·DelegatingRequestProcessor
action配置就无须配置type属性,即使配置了type属性也没用处。
·DelegatingActionProxy
修改action的type属性为:
type="org.springframework.web.struts.DelegatingActionProxy"
2、ActionSupport代替Action使action在程序中手动获得
在Action中访问ApplicationContext两种方法:
·WebApplicationContextUtils工具类
通过ServletContext获得spring容器实例。
·ActionSupport支持类
通过getWebApplicationContext()方法获取ApplicationContext实例。
二、在web.xml文件中加载SPRING容器
两种策略:
1、ServletContextListener实现
支持Servlet 2.3以及以上
·只有一个配置文件applicationContext.xml
·有多个配置文件applicationContext.xml daoContext.xml
2、load-on-startup Servlet实现
不支持Servlet 2.3
·只有一个配置文件applicationContext.xml
·有多个配置文件applicationContext.xml daoContext.xml
一、MVC框架创建ApplicationContext实例,在MVC框架加载时自动创建SPRING容器。
1、由spring容器管理action两种方式:
通过contextConfigLocation属性载入,多个配置文件则用","隔开。
- <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
- <set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" />
- </plug-in>
·DelegatingRequestProcessor
- <controller>
- <set-property property="processorClass"
- value="org.springframework.web.struts.DelegatingRequestProcessor" />
- </controller>
·DelegatingActionProxy
修改action的type属性为:
type="org.springframework.web.struts.DelegatingActionProxy"
2、ActionSupport代替Action使action在程序中手动获得
在Action中访问ApplicationContext两种方法:
·WebApplicationContextUtils工具类
通过ServletContext获得spring容器实例。
·ActionSupport支持类
通过getWebApplicationContext()方法获取ApplicationContext实例。
二、在web.xml文件中加载SPRING容器
两种策略:
1、ServletContextListener实现
支持Servlet 2.3以及以上
·只有一个配置文件applicationContext.xml
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
·有多个配置文件applicationContext.xml daoContext.xml
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/daoContext.xml,/WEB-INF/applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
2、load-on-startup Servlet实现
不支持Servlet 2.3
·只有一个配置文件applicationContext.xml
- <servlet>
- <servlet-name>context</servlet-name>
- <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
·有多个配置文件applicationContext.xml daoContext.xml
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/daoContext.xml,/WEB-INF/applicationContext.xml</param-value>
- </context-param>
- <servlet>
- <servlet-name>context</servlet-name>
- <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>