spring整合struts时创建ApplicationContext的方法
来源:互联网 发布:windows不激活会怎么样 编辑:程序博客网 时间:2024/06/07 07:25
首先简单了解下ApplicationContext这个类,ApplicationContext是Spring的容器,负责管理所有的组件,从业务逻辑层组件到持久层组件,都必须运行在Spring容器中。因此,必须在Web应用启动时,创建Spring的ApplicationContext实例。为了让Spring容器能自动加载,通常有以下一些做法:
一、采用ContextLoaderListener来创建ApplicationContext。
Spring提供ServletContextListener的一个实现类ContextLoaderListener,该类可以作为listener使用,它会在创建时自动查找/WEB-INF/下的applicationContext.xml文件。因此,如果只有一个配置文件,并且文件名为applicationContext.xml,则只需要在web.xml文件中增加如下代码即可:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
如果有多个配置文件需要加载,则使用<context-param>元素来确定配置文件的文件名。由于ContextLoaderListener加载时,会查找名为contextConfigLocation的参数。因此,配置<context-param>时的参数名字应该为contextConfigLocation。如果加载多个配置文件,则在web.xml文件增加如下代码即可:
<context-param>
<!—配置多个文件时需要的参数-->
<param-name>contextConfigLocation</param-name>
<!—需要加载的配置文件,可以有多个,用逗号或空格或分号隔开-->
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/service.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
二、采用load-on-startup Servlet来创建ApplicationContext。
Spring提供了一个特殊的Servlet类:ContextLoaderServlet。该Servlet在启动时,会自动查找/WEB-INF/下的applicationContext.xml文件。为了让ContextLoaderServlet随应用启动而启动,应将此Servlet配置成load-on-startup的Servlet。Load-on-startup的值小一点比较合适,因为要保证ApplicationContext优先创建。如果只有一个配置文件,并且文件名为applicationContext.xml,则在web.xml文件中增加如下代码即可:
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
该Servlet用于提供“后台”服务,作为容器管理应用中的其他bean,不需要响应客户请求,因此无须配置servlet-mapping。
如果有多个配置文件需要加载,则使用<context-param>元素来确定配置文件的文件名。由于ContextLoaderListener加载时,会查找名为contextConfigLocation的参数。因此,配置<context-param>时的参数名字应该为contextConfigLocation。如果加载多个配置文件,则在web.xml文件增加如下代码即可:
<context-param>
<!—配置多个文件时需要的参数-->
<param-name>contextConfigLocation</param-name>
<!—需要加载的配置文件,可以有多个,用逗号或空格或分号隔开-->
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/service.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>
三、采用ContextLoaderPlugIn来创建ApplicationContext。
Spring提供了ContextLoaderPlugIn类,用于在启动时加载配置文件。ContextLoaderPlugIn默认加载的配置文件为servletName-servlet.xml,其中servletName是Struts的ActionServlet对应的Servlet名。例如web.xml中进行如下定义:
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
</servlet>
ContextLoaderPlugIn默认加载/WEB-INF/action-servlet.xml,将该文件作为Spring的配置文件。因此,如果Spring的配置文件只有一个,并且文件名为action-sevlet.xml,则只需在struts-config.xml配置文件中增加如下代码:
<plug-in className="org.springframework.
web.struts.ContextLoaderPlugIn"/>
如果有多个配置文件,则使用<set-property property="" value=""/>元素来加载多个配置文件,如果加载多个配置文件,则在struts-config.xml文件增加如下代码即可:
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
<!—需要加载的配置文件,可以有多个,用逗号或空格或分号隔开-->
value="/WEB-INF/action-servlet.xml,
/WEB-INF/applicationContext.xml" />
</plug-in>
- spring整合struts时创建ApplicationContext的方法
- spring整合struts时创建ApplicationContext的方法
- spring整合struts时创建ApplicationContext的方法
- ssh整合!Struts.xml、hibernate与spring整合的applicationContext.xml、web.xml文件的配置!
- spring的ApplicationContext 与 struts
- Spring , Struts整合方法
- Struts+hibernate+Spring的整合方法
- struts整合spring的3中方法
- struts整合spring的3中方法
- Struts与Spring整合的方法
- struts整合spring的3中方法
- Spring整合Struts的三种方法
- spring整合mybatis的applicationContext.xml配置文件
- MyEclipse创建一个struts+spring+ibatis整合的工程
- MyEclipse创建一个struts+spring+ibatis整合的工程
- struts和spring整合创建Action对象的问题?
- spring 获取 ApplicationContext的方法
- Spring获取ApplicationContext的方法
- Delphi编程中流的使用
- Tkinter教程之Scrollbar篇
- Tkinter教程之Menu篇
- Linux 的目录树
- Tkinter教程之Menubutton篇
- spring整合struts时创建ApplicationContext的方法
- Tkinter教程之Message篇
- 揭开Linux的Swap之谜
- Tkinter教程之OptionMenu篇
- Ext2 文件系统的硬盘布局
- Tkinter教程值PaneWindow
- 修复损坏linux 文件系统
- 附錄 B: EXT2 / EXT3 檔案系統
- 细说GNU/Linux系统的文件及文件系统 (1)