In the previous section, we understand what Maven is, and this section talks about an important application scenario, that is, to separate a SSH project into several separate parts through maven, which is important, refueling.
1. The split and aggregation principle of Maven parent project and sub module.
Problem Description: split SSH project into multiple modules development
1.1、Principle of separation
Create a maven project (POM), and then create three sub modules (Maven moudule), of which three submodules, respectively, Dao, service, web, which separate the content of the three layer into a single project, and furtherThe coupling is reduced, and how to connect them is shown.
Why do we need to create the parent parent project to manage its next three sub modules? And let its sub module inherit him?
Inheritance is to eliminate repetition. If Dao, service, and web are created separately, the contents of each project’s pom.xml file are repeated, such as setting the compiled version, locking the version of the spring, and so on, which can be extracted from the duplicated configuration.The definition of pom.xml in the parent project
The three layers are separately separated. How does the web layer call the service layer code? How does the service layer call the code of the Dao layer?
This can not be done before maven, but with Maven everything is different, the web layer code to call the service layer is really simple, because service is a complete project, so we want to use the other item in the web layer.The code in the list only needs to write the corresponding coordinates through the pom.xml file of maven, and add its jar package to the goal. So, in the picture, ssh-web relies on ssh-service, and ssh-service relies on ssh-dao,The principle is what I said, so you can separate the three layers into separate projects, and take a further extraction of their publicly dependent jar packages to be managed by the parent project, which is the effect of the Maven.
1.2、Principle of polymerization
Project development is usually divided into sub module development. Each module needs to run together to run the whole project. For example, three projects, such as Dao, service, and web, will eventually run an independent war operation
Two. Case realization
Problem Description: use Maven to modularized SSH project, and implement data access from web to Dao layer.
2.1、Creating the maven-parent parent module
After the creation of the parent project
From its directory structure, it can be seen that the parent project itself does not write code, and there is a pom.xml file in which the coordinates corresponding to the common jar in multiple sub modules can be configured in the parent project, and the future submodules will not need to match the general J in the pom.xml.The coordinates of the AR Le,
In the pom.xml of the father project, some duplicated configurations are extracted, such as: locking the version of the jar package, setting up the compiled version, and so on. In general, we do not need our own temporary configuration, the Internet or the company have already written, and each use will be directly lost. Here’s the amount of my collection. Hey
2.2、Create a maven-dao sub module
Point next into the following diagram
Click next, as follows
Click Finish to complete the pom.xml file in the parent project.
Looking at the pom.xml file in ssh_dao, you will find that there is more than one parent node, and the node contained in it is actually the coordinates of the parent project.
Look at the directory structure of ssh_dao
Because it’s on the Dao layer and dealing with the database, then in this project, you need to configure hibernate.hbm.xml and hibernate.cfg.xml, but spring is integrated, so hibernate.cfg.xml isNo need to add applicationContext.xml, which requires the integration of spring and hibernate.
Note: split the applicationContext.xml into one applicationContext-dao.xml, and only Dao is configured in this file.
A few other Student.java, which is not easy to read, is too simple.
StudentTest.javaThis need to explain, because the use of JUnit test here, will be wrong, the error is not to find the JUnit jar package, here we will be very confused, why can not find the jar package, not in the father’s project inside the JUnit jar package has been imported? HereThe reason for the error is the scope of the transfer of dependence.
The parent project is regarded as the A project (hereinafter referred to as A), the sub module ssh_dao is regarded as the B project (hereinafter referred to as B), A relies on JUnit jar package is directly dependent. B inherit A (the actual operation is to fill in the coordinates of A in B). It can also be regarded as a dependency.System, B depends on A dependence on JUnit, A depends on JUnit directly, so the relationship between B and JUnit is called transfer (indirect) dependence. When we know the JUnit of A dependency, JUnit jar packages can be set in A.Cope properties, can be compile, test, and so on, and JUnit is set by test, used only in A test, then B want to use JUnit, JUnit is the scope of the action is also test? There is a relationship. Look at the table.
According to the example above, let’s see what the scope of JUnit is in B. In the first place, B depends on A, directly dependent, and the scope of A in B is compile (not set up by default), so to find a line of compile directly in this column, alsoIt’s a line with a red box frame, then B depends on JUnit, and for A, A is the transfer dependency JUnit, and at this time, what is the scope of the JUnit setting (what is the scope of JUnit in B)? See the transfer dependency on this line, JuniT is set up by test, to find the column of test, to see where the intersection is empty, and to show that JunTi is not able to use the test range in B. In fact, you can’t use JUnit in any scope in the B, so you understand what this picture means. This is just the originalIn fact, when we solve this problem, we use a simple and crude solution. What jar packets are missing, we can import jar packets again.
So in the pom.xml of the ssh_dao sub module, there is a JUnit coordinate to use test.
2.3、Create a ssh_service sub module
Method is the same as the ssh_dao module creation method, the module name is ssh_service.
Looking at the pom.xml files of ssh_service and ssh_parent, there will be the same situation as when ssh_dao is created, with ssh_service more than one parents node, and more than one module knot in ssh_parent.spot
Adding two dependencies to ssh_service’s pom.xml
And then write the service layer code,
The main concern is the relevant code of transactions in applicationContext-service.xml.
The test of this layer requires that the applicationContext-dao.xml in ssh_dao be included in the applicationContext-service.xml of ssh_service before it can be tested. thisThere’s no test in it,
2.4、Create a ssh_web sub module
Method with the maven-dao module creation method, the module name is ssh-web, note: the packaging method is war, and no longer is the jar package, because the layer needs to be put in the Tomcat. Interacting with browsers is the web project, so it is called war package.
As in the previous case, a module node is added to the pom.xml of ssh_parent, and a parent node is added to ssh_web’s pom.xml.
This is also very simple, just like a common Struts2, just a combination of spring, and what objects are given by spring, and there’s one more thing to do with the previous applicationContext configuration file.Look at the next picture
web.xmlThe interceptor of Struts2 and the listener of the spring
Here is a problem. When struts is integrated with spring, class in Struts.xml should fill in ID in spring configuration file.
2.5、Summing up and starting
After the parent project and the submodule have been written, this is the case.
Run and debug. Here I have a small problem that I can’t solve.
Understand the function of module and aggregation, and know how to develop it. Come on。 It’s a bit irritable.