maven (2) maven project builds ssh project (splitting and aggregation of parent project and submodule)

In the previous section, we understood what maven is. In this section, we will talk about one of its important application scenarios, that is, dividing a ssh project into different parts for independent development through maven. It is very important, come on

                              --WH

 

1. The principle of splitting and aggregation of maven parent project and submodule

      Problem description: Split the ssh project into multiple modules for development

      1.1. The principle of splitting

        Create a maven project (pom), and then create three sub-modules (maven module), of which the three sub-modules are dao, service, web, that is, the contents of the three layers are separated into a project, which further reduces the coupling. , how to connect them, see the figure below.

                  

        Why do you need to create a parent project to manage its next three submodules? And let its submodules inherit him?

          Inheritance is to eliminate duplication. If you separate dao, service, and web to create independent projects, the content in the pom.xml file of each project will be duplicated, such as: setting the compiled version, locking the spring version, etc., you can duplicate these The configuration is extracted and defined in the pom.xml of the parent project

        The three layers are separated independently. How does the web layer call the service layer code? How does the service layer call the code of the dao layer?

          This could not be done before maven, but with maven everything is different. The code for the web layer to call the service layer is actually very simple, because the service is a complete project, then we think in the project of the web layer To use the code in the Bide project, you only need to write the corresponding coordinates through the pom.xml file of maven, and add its jar package to achieve the purpose. Therefore, as shown in the figure, ssh-web depends on ssh-service, ssh- Service depends on ssh-dao. The principle is what I said, so it is possible to separate these three layers into independent projects, and further extract the jar packages of its public dependencies, and hand them over to the parent project for management, which maven brings Effect.

 

      1.2, the principle of aggregation

        Project development is usually grouped and divided into modules. After each module is developed, to run the entire project, each module needs to be run together. For example, the three projects of dao, service, and web will eventually run as an independent war.

      

2. Case realization

      Description of the problem: Use maven to divide the ssh project into modules, and implement data access from the web to the dao layer for experimentation

      2.1. Create maven-parent parent module

                  

        Click next

                  

        Click next

                  

        The parent project after creation is as shown in the figure  

              

        It can be seen from its directory structure that the parent project itself does not write code. It contains a pom.xml file. This file can centrally configure the coordinates corresponding to the common jars in multiple submodules in the parent project. Submodules do not need to configure the coordinates of the general jar in pom.xml,

 

        Extract some repeated configurations from the pom.xml of the parent project, such as: locking the version of the jar package, setting the compiled version, etc. Generally, this kind of configuration does not require our own temporary configuration. It has already been written on the Internet or in the company. Just throw it away every time you use it. Below is one of my favorites. Hey-hey

  The maven parent project integrates the common pom.xml configuration for ssh

 

 

      2.2. Create a maven-dao submodule

              

        Click next to enter the following picture

                   

        Click next, as shown below

               

                    

        Click finish, finish, and view the pom.xml file in the parent project

              

        Looking at the pom.xml file in ssh_dao, you will find that there is one more parent node, and the nodes contained inside are actually the coordinates of the parent project

              

 

        View the directory structure of ssh_dao

              

        Because it is in the dao layer and deals with the database, then in this project, hibernate.hbm.xml and hibernate.cfg.xml need to be configured, but spring is integrated, so hibernate.cfg.xml is not needed, add applicationContext .xml is enough (here needs to have the foundation of spring integration hibernate)

                

        Note: split applicationContext.xml into one applicationContext-dao.xml, only dao is configured in this file

  aplicationContext-dao.xml
  jdbcinfo.properties

 

        You don't need to read the other Student.java, it's too simple.

 

        StudentTest.java needs to be explained, because when the junit test is used here, an error will be reported. The error reported is that the junit jar package cannot be found. Here we will be very puzzled, why can't the jar package be found, not in Has the junit jar package been imported into the parent project? The reason for the error here is a scoping issue with transitive dependencies.

        The parent project is regarded as the A project (hereinafter referred to as A), the submodule ssh_dao is regarded as the B project (hereinafter referred to as B), and the jar package that A depends on junit is a direct dependency. B's inheritance of A (the actual operation is to fill in the coordinates of A in B) can also be regarded as a kind of dependency, then it is such a relationship, B depends on A depends on junit, A depends on junit is a direct dependency, then the relationship between B and junit is It is called transitive (indirect) dependency. When we know the junit that A depends on, the jar package of junit can be set in the scope of use in A, which is the scope attribute, which can be compile, test, etc., while junit is set to test, only in A It is used for testing, so when B wants to use junit, is the scope of junit also test? There is a relationship. See the table for details.

                

         按照刚才上面的例子,来看看在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中的事务的相关代码

copy code
<?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">
        <!-- Transaction Manager -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- Transaction Notification-->
    <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>

         <!-- Inject StudentDao in StudentService -->
         <bean id="studentService" class="com.wuhao.ssh_service.service.impl.StudentServiceImpl">
             <property name="studentDao" ref="studentDao"></property>
         </bean>                   
                              
</beans>
copy code

 

            For the test of this layer, the applicationContext-dao.xml in ssh_dao needs to be included in the applicationContext-service.xml of ssh_service to be able to experiment. No testing here,

 

 

      2.4. Create ssh_web submodule

          The method is the same as the maven-dao module creation method. The module name is ssh-web. Note: the packaging method is war, not jar, because this layer needs to be placed in tomcat. Interacting with the browser is a web project, so it is labeled as a war package

          As before, add a module node to pom.xml of ssh_parent, and add a parent node to pom.xml of ssh_web

          This is also very simple, it is the same as writing ordinary struts2, but it is combined with spring. Any objects are given by spring, and there is one more thing to do here, which is to combine the previous applicationContext configuration file, see the picture below

                    

copy code
<?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>
copy code

 

          Configure the interceptor of struts2 and the listener of spring in web.xml

copy code
<?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 configuration file loaded listener -->
    <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 core controller-->
     <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>
copy code

 

          Pay attention to a problem here. When struts is integrated with spring, the class in Struts.xml should fill in the id in the spring configuration file.

 

      2.5. Summary and start

           After the parent project and the submodule are written, this is the case

              

                        

           Just run and debug. I have a small problem here, which I haven't been able to solve.

              

 

3. Summary

      Understand the role of this sub-module and aggregation, and know how to develop it. come on. It was really annoying.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324829645&siteId=291194637