前一節咱們明白了maven是個什麼玩意,這一節就來說講他的一個重要的應用場景,也就是經過maven將一個ssh項目分割爲不一樣的幾個部分獨立開發,很重要,加油java
--WZYmysql
1、maven父工程與子模塊的拆分與聚合原理web
問題描述:將ssh工程拆分爲多個模塊開發spring
1.一、拆分原理sql
建立一個maven project(pom),而後在建立三個子模塊(maven moudule),其中三個子模塊,分別爲 dao、service、web,也就是將三層的內容分別獨立爲一個項目,進一步將耦合性下降,其中如何將他們鏈接起來了,看下圖。數據庫
爲何須要建立parent父工程來管理其下三個子模塊呢?並讓其子模塊繼承他呢?express
繼承是爲了消除重複,若是將dao、service、web分開建立獨立的工程則每一個工程的pom.xml文件中的內容存在重複,好比:設置編譯版本、鎖定spring的版本的等,能夠將這些重複的配置提取出來在父工程的pom.xml中定義apache
將三層都獨立分開來了,web層如何調用service層代碼?service層又如何調用dao層的代碼呢?json
這個在沒有maven以前是不能夠這樣作的,可是有了maven一切都不同了,web層調用service層的代碼其實很簡單,由於service是一個完整的項目,那麼咱們在web層這個項目中想要使用別得項目中的代碼,只須要經過maven的pom.xml文件編寫對應的座標,將其jar包加入進來便可達到目的,所以,看圖中,ssh-web依賴ssh-service,ssh-service依賴ssh-dao,其中的原理就是我說的這樣,因此才能將這三層分開成獨立的項目,而且進一步抽取其公有依賴的jar包,統一交由父工程來管理,這就maven帶來的效果。api
1.二、聚合原理
項目開發一般是分組分模塊開發,每一個模塊開發完成要運行整個工程須要將每一個模塊聚合在一塊兒運行,好比:dao、service、web三個工程最終會打一個獨立的war運行
2、案例實現
問題描述:使用maven將ssh項目進行分模塊,而且實現從web到dao層的數據的存取進行實驗
2.一、建立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.2四、spring4.2.四、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>
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>
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test jdbc.user=root jdbc.password=root
其餘幾個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.三、建立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>
該層的測試,須要將ssh_dao中的applicationContext-dao.xml將ssh_service的applicationContext-service.xml包含進去纔可以實驗的通。這裏不作測試,
2.四、建立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>
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>
這裏注意一個問題,struts跟spring整合的時候,Struts.xml中的class應該填寫spring配置文件中的id。
2.五、總結與啓動
父工程和子模塊都寫完以後,就成這樣了
運行調試便可。這裏我遇到一個小問題,一直解決不了,
3、總結
理解了這個分模塊與聚合的做用,而且知道如何開發便可。加油。確實搞的有點煩躁。