6. Modularization and Decoupling - Maven
Contents
Through the maven modularization mechanism, we can separate different types of functions and store them in different modules.
Create Maven Modules
Create Common Module
- Right click shop (project) directory: New > Module
- Choose “Maven”
- Tick “Create from archetype” checkbox option
- Scroll down a little bit and choose “maven-archetype-quickstart”, click “Next” button

- Fill “common” in ArtifactId, click “Next” buttons till the last step

- Click “Finish” button
Remove “co.dongchen” package in java and test directories of the common module

Create Service Module
- Right click shop (project) directory: New > Module
- Choose “Maven”
- Tick “Create from archetype” checkbox option
- Scroll down a little bit and choose “maven-archetype-quickstart”, click “Next” button

- Fill “service” in ArtifactId, click “Next” buttons till the last step

- Click “Finish” button
Remove “co.dongchen” package in java and test directories of the service module

Create Controller Module
- Right click shop (project) directory: New > Module
- Choose “Maven”
- Tick “Create from archetype” checkbox option
- Scroll down a little bit and choose “maven-archetype-quickstart”, click “Next” button

- Fill “controller” in ArtifactId, click “Next” buttons till the last step

- Click “Finish” button
Remove “co.dongchen” package in java and test directories of the service module

Migrate Files to Correspondent Modules
Migrate Common Module Related Files
- Right click common/src/main/java directory: New > Package
- Fill in “co.dongchen.shop.common.model”
- Click “Finish” button

Drag the Product.java file in from the “shop/src/main/java/co.dongchen.shop.common.model” package, and remove the empty package.
Migrate Service Module Related Files
Service Package
- Right click service/src/main/java directory: New > Package
- Fill in “co.dongchen.shop.service”
- Click “Finish” button

Drag the ProductService.java file in from the “shop/src/main/java/co.dongchen.shop.service” package, and remove the empty package.
Config Package
- Right click service/src/main/java directory: New > Package
- Fill in “co.dongchen.shop.config”
- Click “Finish” button

Drag the DruidConfig.java file in from the “shop/src/main/java/co.dongchen.shop.config” package, and remove the empty package.
Mapper Package
- Right click service/src/main/java directory: New > Package
- Fill in “co.dongchen.shop.mapper”
- Click “Finish” button

Drag the ProductMapper.java file in from the “shop/src/main/java/co.dongchen.shop.mapper” package, and remove the empty package.
Resources Directory
- Right click service/src/main directory: New > Directory
- Fill in “resources”
- Click “Finish” button

Drag both the mapper and mybatis directories in from the “shop/src/main/resources” directory
Migrate Controller Module Related Files
Shop Package
- Right click controller/src/main/java directory: New > Package
- Fill in “co.dongchen.shop”
- Click “Finish” button

Drag the ShopApplication.java file in from the “shop/src/main/java/co.dongchen.shop” package
Controller Package
- Right click controller/src/main/java directory: New > Package
- Fill in “co.dongchen.shop.controller”
- Click “Finish” button

Drag both the FreeMarkerController.java and ProductController.java files in from the “shop/src/main/java/co.dongchen.shop.controller” package, and remove the empty package.
Resources Directory
- Right click controller/src/main directory: New > Directory
- Fill in “resources”
- Click “Finish” button

Drag both the templates directory and the application.properties file in from the “shop/src/main/resources” directory.
Move Maven Dependencies into Related Modules
Common Module
Move the following dependencies from “shop/pom.xml” to “common/pom.xml”:
|
|
Service Module
Move the following dependencies from “shop/pom.xml” to “service/pom.xml”:
|
|
Add the common module dependency into “service/pom.xml”:
|
|
Controller Module
Move the following dependencies from “shop/pom.xml” to “controller/pom.xml”:
|
|
Add the service module dependency into “controller/pom.xml”:
|
|
Verify
Run the App
|
|

Choose the correspondent option and press enter.
View Home Page in the Browser
|
|

Now, our application is modularized.
References Modules
Author Dong Chen
LastMod Mon Apr 29 2019