Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to build multiple modules in springboot

Shulou Source: shulou.com Published: 2022-06-02 10:37:37 09月28日 Update

This article is to share with you about how to build multiple modules in springboot, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

1. Scene description

First introduce the background, why does the project need to use multiple modules? Isn't springmvc enough?

(1) Design pattern motto: "high cohesion, low coupling", springmvc projects generally divide the project into multiple packages: controller, service, dao, util, etc., but as the complexity of the project increases, you want to reuse another module, because it is in the form of a package, it will be more difficult to peel off, and the coupling is a bit strong. The common method is to copy code changes, but this will do a lot of useless work and increase the probability of error.

(2) springboot multi-module to put it simply, is the module by package mode, with the help of maven upgrade to jar way, more abstract, if jar and then upgrade to war or multiple sets of jar, it will become a micro service (springcloud entry series), in multi-module jar mode can be taken out of a jar to share, can greatly improve the code reuse rate and development efficiency.

two。 Solution 2.1 overall thinking

(1) create a new springboot project

(2) create multiple module in the newly built springboot project

(3) modify pom files and delete redundant files and folders.

2.2 New springboot project (springboot project is built quickly)

(1) new- > project

(2) next, change the name.

2.3 New module

(1) right-click on the springboot project-> new- > module

The rest of the way is the same as the springboot way above, and I won't say any more.

(2) create three new module:controller, service and dao. The results are as follows:

2.4 Delete redundant files and modify configuration files (key) 2.4.1 Delete redundant files and folders

(1) springboot project

Delete the src folder overall.

(2) module module

Delete the application startup class under service and dao and the corresponding configuration file application.yml/prpperty, and leave the cotroller module unchanged.

2.4.2 modify pom.xml

According to the springmvc architecture, several module depend on the order controller- > service- > dao

(1) modify the outermost pom.xml of springboot

This is the parent pom.xml, used to load some global or public jar packages, as well as configuration packaging.

There are two things that need to be changed in this pom file:

One is to change the packaging mode to pom.

The second is to create a new modules tag to add 3 module.

As follows:

Pom controller service dao

(2) modify the pom.xml file of cotroller

Modify the label to be the gav information of the springboot project of this project and the jar package information that depends on service.

Com.laowang lwmodul 0.0.1-SNAPSHOT com.laowang service 0.0.1-SNAPSHOT

(3) modify the pom.xml file of service

Similar to controller, except that the dependency is changed to dao.

Com.laowang lwmodul 0.0.1-SNAPSHOT com.laowang dao 0.0.1-SNAPSHOT

(4) modify the pom.xml file of dao

You only need to modify the parent, and you no longer need to configure dependencies.

Com.laowang lwmodul 0.0.1-SNAPSHOT

(5) start

Start with the startup class ControllerApplication in the Cotroller module. If you have an empty project, you can see this line to show that it is successful.

Started ControllerApplication in 2.485 seconds (JVM running for 3.639) 2.5 adds web access authentication 2.5.1 profile

(1) change the application.property under controller to the lower port number (default: 8080 if you don't change it).

Server.port=9000

(2) increase dependence

Org.springframework.boot spring-boot-starter-web2.5.2 startup class

Add a label (@ RestController) and a request method (home ()) to the startup class ControllerApplication.

Package com.laowang.controller;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@SpringBootApplicationpublic class ControllerApplication {public static void main (String [] args) {SpringApplication.run (ControllerApplication.class, args) } @ RequestMapping ("/") public String home () {return "iTunm Software Wang, welcome!";} 2.5.3 renderings

The above is how to build multiple modules in springboot. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Files projects modules configurations patterns multiple that is folders methods tags facets code information effects renderings totals methods more knowledge articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Redmi Shulou Tech Info Microsoft