---
title: "在struts2.0框架中,多struts.xml与多applicationContext.xml配置的方法"
author: "devsky"
date: "2011-03-01T13:27:07Z"
tags: []
categories: ["Web前端"]
slug: "struts2-0-struts-xml-applicationcontext-xml"
draft: false
lastmod: "2011-03-01T13:27:07Z"
id: 935494
url: "https://devsky.iteye.com/blog/935494"
---

在用SSH2做项目中，一般都是在一个struts.xml中配置控制转发，而用一个applicationContext.xml用来管理Bean。这对于一个简单的项目来说，就已经足够了。但是，对于一个大型项目的来说，团队成员很多，每个成员都在这两个配置文件里各写各的配置信息，这样会使这两个文件信息量过大，整合起来也不是很方便，阅读代码很困难。鉴于这种情况，是否有更好的解决办法呢？上网查了很久，也试了很久，终于有解决的办法了。下面就将我的配置信息罗列出来，供大家参考。

 

**1.**首先在src下建立一个总的struts.xml文件，该文件的作用是，加载每一个模块的配置文件信息(换句话说就是加载多个struts.xml文件),文件的内容如下：  
&lt;?xml version="1.0" encoding="UTF-8" ?&gt;  
&lt;!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
    "[http://struts.apache.org/dtds/struts-2.0.dtd](http://struts.apache.org/dtds/struts-2.0.dtd)"&gt;  
&lt;struts&gt;  
    &lt;include file="struts-default.xml" /&gt;

    &lt;!--模块1 struts的XML文件--&gt;  
    &lt;include file="config/struts-basicManager.xml" /&gt;  
     
    &lt;!--模块2 struts的XML文件--&gt;  
    &lt;include file="config/struts-inspection.xml" /&gt;  
     
    &lt;!--模块3 struts的XML文件--&gt;  
    **&lt;include file="config/struts-curingManager.xml" /&gt;**  
      
     
&lt;/struts&gt;

这样总的struts.xml文件就配置好了。

 

**2.**在src下新件一个包，包名为config。该包的作用是存放每一个分模块的struts的配置文件。例如我的一个配置文件是struts-curingManager.xml,内容如下：

&lt;?xml version="1.0" encoding="UTF-8" ?&gt;  
&lt;!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
    "[http://struts.apache.org/dtds/struts-2.0.dtd](http://struts.apache.org/dtds/struts-2.0.dtd)"&gt;  
&lt;struts&gt;  
    &lt;package name="qdbridge" extends="struts-default" &gt;  
     &lt;action name="ceshi" class="com.xtlh.qdbridge.action.Component\_ReduceAction" method="list"&gt;  
            &lt;result name="success" &gt;/MyJsp.jsp&lt;/result&gt;  
        &lt;/action&gt;   
 &lt;/package&gt;  
&lt;/struts&gt;

这个文件可以用来写多个action。

 

**3.**接下来就是配置总的applicationContext.xml文件了。  
首先，为了管理方便，我在WEB-INF下新建一个conf文件夹，将WEB-INF下的applicationContext.xml文件拖到conf文件夹下，这样作的目的是方便管理。内容如下：

&lt;?xml version="1.0" encoding="UTF-8" ?&gt;  
&lt;beans  
 xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"  
 xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
 xsi:schemaLocation="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)  
 [http://www.springframework.org/schema/beans/spring-beans-2.5.xsd](http://www.springframework.org/schema/beans/spring-beans-2.5.xsd)"&gt;  
     
    **&lt;import  resource="applicationContext-shenwen.xml"/&gt;**  
    &lt;!-- 配置数据源 --&gt;  
 &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"&gt;  
      &lt;property name="driverClass"&gt;  
      &lt;value&gt;oracle.jdbc.driver.OracleDriver&lt;/value&gt;  
      &lt;/property&gt;  
      &lt;property name="jdbcUrl"&gt;  
           &lt;value&gt;jdbc:oracle:thin:@192.168.0.47:1521:qdhw&lt;/value&gt;  
      &lt;/property&gt;  
      &lt;property name="user"&gt;  
          &lt;value&gt;qdhw&lt;/value&gt;  
      &lt;/property&gt;  
      &lt;property name="password"&gt;  
          &lt;value&gt;qdhw&lt;/value&gt;  
      &lt;/property&gt;  
      &lt;property name="minPoolSize"&gt;  
          &lt;value&gt;15&lt;/value&gt;  
      &lt;/property&gt;  
      &lt;property name="acquireIncrement"&gt;  
          &lt;value&gt;5&lt;/value&gt;  
      &lt;/property&gt;  
      &lt;property name="maxPoolSize"&gt;  
          &lt;value&gt;25&lt;/value&gt;  
      &lt;/property&gt;  
 &lt;/bean&gt;  
     
    &lt;!-- 配置SessionFactory --&gt;  
 &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;  
  &lt;property name="dataSource" ref="dataSource" /&gt;  
  &lt;!-- 指定HIbernate映射文件的路径 --&gt;  
  &lt;property name="mappingDirectoryLocations"&gt;  
   &lt;list&gt;  
    &lt;value&gt;classpath:com/xtlh/qdbridge/entity&lt;/value&gt;  
   &lt;/list&gt;  
  &lt;/property&gt;  
  &lt;!-- 配置Hibernate的属性 --&gt;  
  &lt;property name="hibernateProperties"&gt;  
   &lt;props&gt;  
    &lt;prop key="hibernate.dialect"&gt;  
     org.hibernate.dialect.Oracle9Dialect  
    &lt;/prop&gt;  
    &lt;prop key="hibernate.show\_sql"&gt;true&lt;/prop&gt;  
   &lt;/props&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;  
   
   
     
&lt;/beans&gt;

大家会注意到，这里我用到了c3p0这个数据源连接池，目的是建立多连接，提高效率。在数据源的上方会看到有这么一句话，&lt;import  resource="applicationContext-shenwen.xml"/&gt;，这句话的意思

就是在总的applicationContext.xml中引入各个子模块的applicationContext配置文件，它的作用相当于java中的导包。需要注意的是，&lt;import  resource="applicationContext-shenwen.xml"/&gt;这一

句话，要放在所有bean配置的最前面。

 

**4.**同样，在WEB-INF目录下的conf文件夹下，新建一个子模块applicationContext配置文件(可以建立多个)，我的文件名是：applicationContext-shenwen.xml，内容如下：

&lt;?xml version="1.0" encoding="UTF-8" ?&gt;  
&lt;beans  
 xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"  
 xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
 xsi:schemaLocation="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)  
 [http://www.springframework.org/schema/beans/spring-beans-2.5.xsd](http://www.springframework.org/schema/beans/spring-beans-2.5.xsd)"&gt;  
   
 &lt;bean id="crDao" class="com.xtlh.qdbridge.springImpl.Component\_ReduceDaoImpl" &gt;  
   &lt;property name="sessionFactory"&gt;  
    **&lt;ref bean="sessionFactory"/&gt;**  
   &lt;/property&gt;  
  &lt;/bean&gt;  
&lt;/beans&gt;

大家会看到，这其实和总的applicationContext.xml一样，但是请大家注意黑体部分是不一样的，以前是&lt;ref local="sessionFactory"/&gt;现在变成了&lt;ref bean="sessionFactory"/&gt;。不然会出错的。这

里可以注入多个Dao接口。

 

5.启动Tomcat，会发现报错。这时不要急，就差一步了。是因为，刚才在第三步，把总的applicationContext.xml拖到了conf文件夹下，系统找不到，这时只要在web.xml中配置一下就可以了。黑体内容

如下：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;web-app version="2.5"  
 xmlns="[http://java.sun.com/xml/ns/javaee](http://java.sun.com/xml/ns/javaee)"  
 xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
 xsi:schemaLocation="[http://java.sun.com/xml/ns/javaee](http://java.sun.com/xml/ns/javaee)  
 [http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd](http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd)"&gt;  
  &lt;welcome-file-list&gt;  
    &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;  
  &lt;/welcome-file-list&gt;  
   
  &lt;filter&gt;  
  &lt;filter-name&gt;struts2&lt;/filter-name&gt;  
  &lt;filter-class&gt;org.apache.struts2.dispatcher.FilterDispatcher&lt;/filter-class&gt;  
  &lt;/filter&gt;  
  &lt;filter-mapping&gt;  
  &lt;filter-name&gt;struts2&lt;/filter-name&gt;  
  &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
  &lt;/filter-mapping&gt;  
   
  **&lt;context-param&gt;**   
  **&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;**   
  **&lt;param-value&gt;/WEB-INF/conf/applicationContext.xml&lt;/param-value&gt;**   
  **&lt;/context-param&gt;**  
   
   
  &lt;!-- log4j配置 --&gt;  
  &lt;context-param&gt;    
    &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt;    
    &lt;param-value&gt;/WEB-INF/log4j.properties&lt;/param-value&gt;    
  &lt;/context-param&gt;    
   
  &lt;context-param&gt;    
 &lt;param-name&gt;log4jRefreshInterval&lt;/param-name&gt;    
 &lt;param-value&gt;60000&lt;/param-value&gt;    
  &lt;/context-param&gt;  
   
  &lt;listener&gt;    
 &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/listener-class&gt;    
 &lt;/listener&gt;  
    
  &lt;listener&gt;  
     &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;  
  &lt;/listener&gt;  
   
&lt;/web-app&gt;

这样就一切OK了。