随笔-16  评论-84  文章-1  trackbacks-0

Web应用初始化Spring容器策略

以下软件测试环境为Spring,Struts1

1、通过struts1提供的插件机制,采用Spring提供的ContextLoaderPlugIn

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
 <global-exceptions />
 <global-forwards />
 <message-resources parameter="com.portal.ApplicationResources" />
 <!--  通过S1提供的插件机制来初始化Spring容器,加载Spring配置文件
 <plug-in
  className="org.springframework.web.struts.ContextLoaderPlugIn">
  <!--
   1、ContextLoaderPlugIn默认加载配置文件命名规则是actionServlet-servlet.xml,其中actionServlet
   是配置org.apache.struts.action.ActionServlet时指定的servlet名称
   
   2、通过配置contextConfigLocation属性来指点Spring配置文件的位置,多个配置文件可以使用 逗号","、分号";"、空格" "
  -->
  <set-property property="contextConfigLocation"
   value="/WEB-INF/conf/spring-application.xml,/WEB-INF/conf/**/spring*.xml" />
 </plug-in> 
  -->
</struts-config>


2、采用Spring提供的ContextLoaderListener来初始化(应用服务器需要支持Listener,Servlet2.3版本及以上)
 <context-param>
   <description>通过配置contextConfigLocation属性来指点Spring配置文件的位置,多个配置文件可以使用 逗号","、分号";"、空格" "</description>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/conf/spring-application.xml /WEB-INF/conf/**/spring*.xml</param-value>
  </context-param>
 
  <listener>
   <description>通过ContextLocaderListener来初始化Spring容器,加载Spring配置文件</description>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

3、采用load-on-startup Servlet 来初始化Spring容器
  <servlet>
   <description>
    通过load-on-startup Servlet来初始化Spring容器
    该如何Servlet 用于提供"后台"服务,作为容器管理应用中的其他bean,不需要响应客户请求,因此无须配置servlet-mapping
   </description>
   <servlet-name>applicationContext</servlet-name>
   <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
   <load-on-startup>0</load-on-startup>
  </servlet>

posted on 2010-09-13 18:10 absolute 阅读(2244) 评论(0)  编辑  收藏 所属分类: OpenSource

只有注册用户登录后才能发表评论。


网站导航: