1. 程式人生 > >maven(二) maven項目構建ssh工程(父工程與子模塊的拆分與聚合)

maven(二) maven項目構建ssh工程(父工程與子模塊的拆分與聚合)

子模塊 mbo warnings 找不到 .cn scope spl template opened

        前一節我們明白了maven是個什麽玩意,這一節就來講講他的一個重要的應用場景,也就是通過maven將一個ssh項目分割為不同的幾個部分獨立開發,很重要,加油

                              --WH

一、maven父工程與子模塊的拆分與聚合原理

      問題描述:將ssh工程拆分為多個模塊開發

      1.1、拆分原理

        創建一個maven project(pom),然後在創建三個子模塊(maven moudule),其中三個子模塊,分別為 dao、service、web,也就是將三層的內容分別獨立為一個項目,進一步將耦合性降低,其中如何將他們連接起來了,看下圖。

                  技術分享

        為什麽需要創建parent父工程來管理其下三個子模塊呢?並讓其子模塊繼承他呢?

          繼承是為了消除重復,如果將dao、service、web分開創建獨立的工程則每個工程的pom.xml文件中的內容存在重復,比如:設置編譯版本、鎖定spring的版本的等,可以將這些重復的配置提取出來在父工程的pom.xml中定義

        將三層都獨立分開來了,web層如何調用service層代碼?service層又如何調用dao層的代碼呢?

          這個在沒有maven之前是不可以這樣做的,但是有了maven一切都不一樣了,web層調用service層的代碼其實很簡單,因為service是一個完整的項目,那麽我們在web層這個項目中想要使用別得項目中的代碼,只需要通過maven的pom.xml文件編寫對應的坐標,將其jar包加入進來即可達到目的,因此,看圖中,ssh-web依賴ssh-service,ssh-service依賴ssh-dao,其中的原理就是我說的這樣,所以才能將這三層分開成獨立的項目,並且進一步抽取其公有依賴的jar包,統一交由父工程來管理,這就maven帶來的效果。

      1.2、聚合原理

        項目開發通常是分組分模塊開發,每個模塊開發完成要運行整個工程需要將每個模塊聚合在一起運行,比如:dao、service、web三個工程最終會打一個獨立的war運行

      

二、案例實現

      問題描述:使用maven將ssh項目進行分模塊,並且實現從web到dao層的數據的存取進行實驗

      2.1、創建maven-parent父模塊

              技術分享    

        點擊next

              技術分享    

        點擊next

              技術分享    

        創建好之後的父工程如圖  

              技術分享

        從它的目錄結構可以看出,父工程本身不寫代碼,它裏面有一個pom.xml文件,這個文件可以將多個子模塊中通用的jar所對應的坐標,集中在父工程中配置,將來的子模塊就可以不需要在pom.xml中配置通用jar的坐標le、

        在父工程的pom.xml中抽取一些重復的配置的,比如:鎖定jar包的版本、設置編譯版本等,一般這種都不需要我們自己臨時配置,網上或者公司都有已經寫好了的,每次使用就直接丟過來即可。下面給一個我收藏的額。嘿嘿

技術分享
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.wuhao.sshtest</groupId>
  <artifactId>ssh_parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  
  <!-- 為了確定每個框架的版本號 -->

  <!-- 鎖定版本 -->
      <properties>
         <spring.version>4.2.4.RELEASE</spring.version>
        <struts2.version>2.3.24</struts2.version>
        <hibernate.version>5.0.7.Final</hibernate.version>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <shiro.version>1.2.3</shiro.version>
        <cxf.version>3.0.1</cxf.version>
        <c3p0.version>0.9.1.2</c3p0.version> 
        <mysql.version>5.1.6</mysql.version>
    </properties>
      <!-- 鎖定版本,struts2-2.3.24、spring4.2.4、hibernate5.0.7 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>${hibernate.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-core</artifactId>
                <version>${struts2.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-spring-plugin</artifactId>
                <version>${struts2.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>
  
  <dependencies>
      <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.4</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.37</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.1</version>
        </dependency>
        <!-- <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency> -->
        <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.1</version>
        </dependency>
    
        <!-- jstl -->
         <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
        
        <!-- shiro -->
         <!-- apache shiro dependencies -->
         <!-- <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-all</artifactId>
            <version>${shiro.version}</version>
         </dependency> -->

      
       <!-- spring -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8</version>
        </dependency>
        
         <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.version}</version>
        </dependency>
        
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
          <version>${spring.version}</version>
        </dependency>
        
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>${spring.version}</version>
        </dependency>

        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-orm</artifactId>
          <version>${spring.version}</version>
        </dependency>
        
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>${spring.version}</version>
        </dependency>
        
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${spring.version}</version>
        </dependency>
        
    
        
        <!-- struts2  begin -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>${struts2.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>javassist</artifactId>
                    <groupId>javassist</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>${struts2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>${struts2.version}</version>
        </dependency>
        <dependency>
              <groupId>org.apache.struts</groupId>
              <artifactId>struts2-convention-plugin</artifactId>
              <version>${struts2.version}</version>
          </dependency>
        <!-- struts2  end -->
        
        <!-- hibernate begin -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.1.Final</version>
        </dependency>
        <!-- hibernate end -->
        
        <dependency>
              <groupId>c3p0</groupId>
              <artifactId>c3p0</artifactId>
              <version>${c3p0.version}</version>
          </dependency>
        
        <!-- <dependency> 
            <groupId>org.apache.cxf</groupId> 
            <artifactId>cxf-rt-frontend-jaxws</artifactId> 
            <version>${cxf.version}</version> 
        </dependency> 
        <dependency> 
            <groupId>org.apache.cxf</groupId> 
            <artifactId>cxf-rt-transports-http</artifactId> 
            <version>${cxf.version}</version> 
        </dependency> -->
        
        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- log end -->
        
        <!-- Javamail -->
    <!--     <dependency>
          <groupId>javax.mail</groupId>
          <artifactId>mail</artifactId>
          <version>1.4.4</version>
        </dependency> -->
        
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
    
        <dependency>
            <groupId>org.codehaus.xfire</groupId>
            <artifactId>xfire-core</artifactId>
            <version>1.2.6</version>
        </dependency> 
        
        <dependency>
           <groupId>dom4j</groupId>
           <artifactId>dom4j</artifactId>
           <version>1.6</version>
        </dependency>
    
        <!-- <dependency> 
            <groupId>org.apache.poi</groupId> 
            <artifactId>poi</artifactId> 
            <version>3.11</version> 
        </dependency>
        <dependency> 
            <groupId>org.apache.poi</groupId> 
            <artifactId>poi-ooxml</artifactId> 
            <version>3.11</version> 
        </dependency>
        <dependency> 
            <groupId>org.apache.poi</groupId> 
            <artifactId>poi-ooxml-schemas</artifactId> 
            <version>3.11</version> 
        </dependency> -->
    
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
       
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
       <!--  <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>10.2.0.4.0</version>
        </dependency> -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.6.6</version>
        </dependency>
  </dependencies>
  
   <build>
     <finalName>sshtest</finalName>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
  </build>
  
</project>
maven父工程整合ssh通用的pom.xml配置

      2.2、創建maven-dao子模塊

              技術分享

        點next進入如下圖

              技術分享     

        點擊next,如下圖

              技術分享 

                    

        點擊finish,完成,查看父工程中的pom.xml文件

              技術分享

        查看ssh_dao中的pom.xml文件,會發現多了一個 parent結點,並且內部所包含的結點,其實就是父工程的坐標

              技術分享

        查看ssh_dao的目錄結構

              技術分享

        因為是在dao層,和數據庫打交道,那麽就在這個項目中,需要配置hibernate.hbm.xml和hibernate.cfg.xml,但是又集成了spring,所以hibernate.cfg.xml就不需要了,添加applicationContext.xml即可(這裏需要有spring整合hibernate的基礎)

                技術分享

        註意:將applicationContext.xml拆分出一個applicationContext-dao.xml,此文件中只配置dao

技術分享
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                              http://www.springframework.org/schema/beans/spring-beans.xsd
                              http://www.springframework.org/schema/tx 
                              http://www.springframework.org/schema/tx/spring-tx.xsd
                              http://www.springframework.org/schema/aop 
                              http://www.springframework.org/schema/aop/spring-aop.xsd
                              http://www.springframework.org/schema/context 
                              http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 1 加載properties文件 -->
    <context:property-placeholder location="classpath:jdbcinfo.properties"/>
    <!-- 2 配置數據源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 3 配置hibernate SessionFactory 註意:這裏使用的是hibernate5,要看你使用的jar包是什麽版本的,每個人不一樣。-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 3.1 配置數據源 
            * <property name="屬性名" ref="另一個bean引用">
                name 必須是對象的setter方法推斷獲得,setDataSource(...), 去掉set DataSource ,首字母小寫  dataSource
            * ref 其他bean引用 <bean id=""> 可以任意,一般情況與上面屬性名稱相同。
        -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 3.2 配置hibernate其他屬性 
            * 在hibernate.cfg.xml 配置文件 “hibernate.dialect” 和 “dialect” 等效的
            * 在spring配置文件中,必須使用“hibernate.dialect”
        -->
        <property name="hibernateProperties">
            <props>
                <!-- 方言 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <!-- 顯示sql語句 -->
                <prop key="hibernate.show_sql">true</prop>
                <!-- 格式化sql語句 -->
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
        <!-- 3.3 加載映射文件 
            com/wuhao/ssh_dao/Student/domain/Student.hbm.xml
            com/wuhao/ssh_dao/xxx/domain/xxx.hbm.xml
            com/wuhao/ssh_dao/*/domain/*.hbm.xml
            <property name="mappingLocations" value="classpath:com/wuhao/ssh_dao/domain/*.hbm.xml"></property>
        -->
         <property name="mappingLocations" value="classpath:com/wuhao/ssh_dao/domain/Student.hbm.xml"></property> 
    </bean>
    

     <!-- hibernate中通過sessionFactory創建得session對對象進行操作,spring提供一個hibernateTemplate進行操作,跟spring中的jdbcTemplate一樣,
     繼承HibernateDaoSupport後,註入SessionFactory即可,在dao層就不用在寫hibernateTemplate的set方法了。
        -->
    <bean id="studentDao" class="com.wuhao.ssh_dao.dao.impl.StudentDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
</beans>
aplicationContext-dao.xml 技術分享
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test
jdbc.user=root
jdbc.password=root
jdbcinfo.properties

        其他幾個Student.java這些就不用看了,太簡單了。

        StudentTest.java這個需要講解一下,因為這裏使用junit測試的時候,會報錯,報的錯誤是找不到junit的jar包,這裏我們就會很疑惑,為什麽會找不到該jar包呢,不是在父工程裏面都導入了junit的jar包了嗎?這裏出錯的原因是傳遞依賴的範圍問題。

        將父工程看做A項目(下面簡稱A),將該子模塊ssh_dao看做B項目(下面簡稱B),A依賴junit的jar包是直接依賴。B繼承A(實際操作就是B中填寫A的坐標)也可以看成一種依賴,那麽就是這樣一種關系,B 依賴 A 依賴 junit, A依賴junit是直接依賴沒錯,那麽B跟junit的關系就叫做傳遞(間接)依賴,我們知道A依賴的junit時,junit的jar包可以設置在A中的使用範圍,就是scope屬性,可以為compile,test等,而junit設置的是test,只在A中測試的時候用,那麽B想用junit時,junit的作用範圍是不是也是test呢?這就有一種關系。具體看表。

                技術分享

         按照剛才上面的例子,來看看在B中,junit的作用範圍是什麽?首先看B 依賴 A,直接依賴,並且A在B中的作用範圍是compile(沒設置就默認),所以在直接依賴這一列中找到compile這一行,也就是用紅色框框框起來的一行,然後B 依賴 junit,對A來說,A 是傳遞依賴 junit,這時候看junit設置的作用範圍是多少(也就是看junit在B中的使用範圍是什麽)?看傳遞依賴這一行,junit設置的是test,找到test這一列,看相交的地方,是空的,則說明,junti在B中的test範圍不能夠使用,其實看圖,B中任何範圍內都不能夠使用junit,這樣你就理解了這張圖是什麽意思。這只是原理,實際上我們解決這種問題的時候,用一個簡單粗暴的解決方案。什麽jar包丟失了,我們就再次導入一次jar包即可。

          所以在ssh_dao子模塊的pom.xml中有junit的坐標才能使用test

                技術分享

                               

        

      2.3、創建ssh_service子模塊

          方法同ssh_dao模塊創建方法一樣,模塊名稱為ssh_service。

          看ssh_service和ssh_parent的pom.xml文件,會出現和ssh_dao創建時一樣的情況,ssh_service多出一個parents結點,ssh_parent多個一個module結點

                技術分享技術分享

          在ssh_service的pom.xml中添加兩個依賴

                技術分享

           然後編寫service層的代碼,

                 技術分享   

           主要關註一下applicationContext-service.xml中的事務的相關代碼

技術分享
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                              http://www.springframework.org/schema/beans/spring-beans.xsd
                              http://www.springframework.org/schema/tx 
                              http://www.springframework.org/schema/tx/spring-tx.xsd
                              http://www.springframework.org/schema/aop 
                              http://www.springframework.org/schema/aop/spring-aop.xsd
                              http://www.springframework.org/schema/context 
                              http://www.springframework.org/schema/context/spring-context.xsd">
        <!-- 事務管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!--  事務通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!-- aop -->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* cn.wuhao.ssh_service.service.impl.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
    </aop:config>

         <!-- 在StudentService中註入StudentDao -->
         <bean id="studentService" class="com.wuhao.ssh_service.service.impl.StudentServiceImpl">
             <property name="studentDao" ref="studentDao"></property>
         </bean>                   
                              
</beans>
aplicationContext-service.xml

            該層的測試,需要將ssh_dao中的applicationContext-dao.xml將ssh_service的applicationContext-service.xml包含進去才能夠實驗的通。這裏不做測試,

      2.4、創建ssh_web子模塊

          方法同maven-dao模塊創建方法,模塊名稱為ssh-web,註意:打包方式為war,而不再是jar包了,因為該層需要放到tomcat中去。與瀏覽器交互,就是web項目了,所以打成war包

          和前面一樣,ssh_parent的pom.xml中增加一個module結點,而ssh_web的pom.xml中增加一個parent結點

          這個也很簡單,就是跟寫普通的struts2是一樣的,只不過是和spring的結合,有什麽對象,都通過spring來給予,並且這裏多做一個事情,就是將之前的applicationContext配置文件進行結合,看下圖

                    技術分享

技術分享
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"   
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans.xsd    
    http://www.springframework.org/schema/aop    
    http://www.springframework.org/schema/aop/spring-aop.xsd    
    http://www.springframework.org/schema/tx    
    http://www.springframework.org/schema/tx/spring-tx.xsd    
    http://www.springframework.org/schema/context    
    http://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="classpath:applicationContext-dao"/>
    <import resource="classpath:applicationContext-service"/>
    <import resource="classpath:applicationContext-web"/>
</beans>
application.xml

          web.xml中配置struts2的攔截器和spring的監聽器

技術分享
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!--spring配置文件的加載的監聽 器-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>

    </context-param> 
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    
     <!--2.懶加載   OpenSessionInviewFilter   noSession or session is closed-->
    <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>

        </init-param>
        <init-param>
            <param-name>sessionFactoryBeanName</param-name>
            <param-value>sessionFactory</param-value>
        </init-param>
    </filter> 
    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>

    </filter-mapping> 
    
    
    
      <!--3.struts2核心控制器-->
     <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter> 
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 

</web-app>
web.xml

          這裏註意一個問題,struts跟spring整合的時候,Struts.xml中的class應該填寫spring配置文件中的id。

      2.5、總結與啟動

           父工程和子模塊都寫完之後,就成這樣了

              技術分享

                        

           運行調試即可。這裏我遇到一個小問題,一直解決不了,

              

三、總結

      理解了這個分模塊與聚合的作用,並且知道如何開發即可。加油。確實搞的有點煩躁。

maven(二) maven項目構建ssh工程(父工程與子模塊的拆分與聚合)