Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the configuration method of Spring Boot multi-module and multi-environment

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what is the configuration method of Spring Boot multi-module and multi-environment". In the daily operation, I believe that many people have doubts about the configuration method of Spring Boot multi-module and multi-environment. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the configuration method of Spring Boot multi-module and multi-environment". Next, please follow the editor to study!

Multiple modules

Module

In modular programming, developers decompose the program into discrete function blocks (discrete chunks of functionality), which are called modules.

Advantages of multiple modules

Each module has the characteristic of high cohesion, which makes it easy to check, debug and test. Well-written modules provide reliable abstraction and encapsulation boundaries, so that each module has a well-organized design and a clear purpose.

Implement multiple modules

Create a maven project

Configure multiple Modul

Add module dependencies

Create a maven project

Configure multiple Modul

In pom, add modules node, module name can be configured as many as possible.

Seckill-api seckill-biz

Usually at this point, multiple modules are configured. But in reality, our modules need to be interdependent, and each module also depends on third-party modules.

Add module dependencies

Seckill-api (api layer) depends on seckill-biz (Business layer). In the pom file of the api module, add the following configuration

Com.sifou.courses seckill-biz 1.0-SNAPSHOT

Assuming that both api and biz modules rely on lombok,validation-api, two third-party modules (packages), how can they be implemented?

Option 1: add dependencies in each (biz & api) module

Option 2: add dependencies to the parent module

I believe everyone will choose option 2; in the pom file in the root project, add the following configuration

1.18.8 2.0.1.Final org.projectlombok lombok ${lombok.version} javax.validation validation-api ${javax.validation}

At this point, the configuration is complete; (is it very clear, please put in coins and like it)

You can also use the mvn dependency:tree command to view dependencies (essential core skills, resolve package conflicts, resolve package version failures)

[INFO] Scanning for projects... [INFO]-[INFO] Reactor Build Order: [INFO] [INFO] com.sifou.courses.seckill [INFO] seckill-biz [INFO] seckill-api [INFO] [INFO]-[INFO] Building com.sifou.courses .seckill 1.0-SNAPSHOT [INFO]-[INFO] [INFO]-maven-dependency-plugin:3.1.2:tree (default-cli) @ com.sifou .courses.seckill-[INFO] com.sifou.courses:com.sifou.courses.seckill:pom:1.0-SNAPSHOT [INFO] +-org.projectlombok:lombok:jar:1.18.8:compile [INFO]\-javax.validation:validation-api:jar:2.0.1.Final:compile [INFO] [ INFO]-[INFO] Building seckill-biz 1.0-SNAPSHOT [INFO]- -[INFO] [INFO]-maven-dependency-plugin:3.1.2:tree (default-cli) @ seckill-biz-[INFO] com.sifou.courses:seckill-biz:jar:1.0-SNAPSHOT [INFO] +-org.projectlombok:lombok:jar:1 .18.8: compile [INFO]\-javax.validation:validation-api:jar:2.0.1.Final:compile [INFO] [INFO]- [INFO] Building seckill-api 1.0-SNAPSHOT [INFO]-[ INFO] [INFO]-- maven-dependency-plugin:3.1.2:tree (default-cli) @ seckill-api-- [INFO] com.sifou.courses:seckill-api:jar:1.0-SNAPSHOT [INFO] +-com.sifou.courses:seckill-biz:jar:1.0-SNAPSHOT:compile [INFO] +-org.projectlombok:lombok:jar:1.18.8:compile [INFO]\-javax.validation:validation-api:jar: 2.0.1.Final:compile [INFO]-[INFO] Reactor Summary: [INFO] [INFO] com.sifou.courses.seckill. . SUCCESS [1.007 s] [INFO] seckill-biz.. SUCCESS [0.040 s] [INFO] seckill-api.. SUCCESS [0.036 s] [INFO]-[INFO] BUILD SUCCESS [INFO]- -[INFO] Total time: 1.862 s [INFO] Finished at: 2020-10-29T23:18:09+08:00 [INFO] Final Memory: 27M/230M [INFO]-- -Process finished with exit code 0

Multi-environment

In our work, we are faced with many environments, such as development, testing, production, and so on. in order to realize multiple environments perfectly, we can take a total of two big steps.

Support multi-environment configuration in the project

Realize multi-environment startup in real environment

Support for multi-environment configuration

Create a properties file

Specify environment parameters

Create a properties file

Create three files with the suffix properties under the resources folder

For example:

Application-dev.properties: development environment

Application-test.properties: test environment

Application-prod.properties: production environment

Specify environment parameters

Spring.profiles.active=test

At this point, the multi-environment configuration is complete.

In Spring Boot, the multi-environment configuration file name must meet the fixed format of application- {profile} .properties, where {profile} corresponds to your environment ID.

For example:

Application-dev.properties: development environment

Application-test.properties: test environment

Application-prod.properties: production environment

Application.properyies activates one or more configuration files through spring.profiles.active. If no configuration file for profile is specified, spring boot will launch application-default.properties; by default and which configuration file will run:

Spring.profiles.active=test

The contents of the application-test.properties configuration file will be loaded

Multi-environment startup

I just talked about how to configure it in a project, so how to start it in a real environment? Is it possible to change the configuration? Of course not, the correct solution is as follows.

-Dspring.profiles.active=$ {PROFILE}

In the startup script, add the above to specify the configuration file to be loaded by environment

At this point, the study on "what is the configuration method of Spring Boot multi-module and multi-environment" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report