This blog will introduce you to multi-environment configuration, swagger and other related content.
Multi-environment configuration analysis
In actual development, we may involve some environments, such as local (local development environment), dev (deployed to remote development environment), test (pre-release environment), product (production environment). Obviously, under different environments, corresponding to different configuration information, such as db/redis/mq, etc., even under different environments, the program needs to do different processing. So how does the springboot project support multi-environment configuration?
First of all, springboot is an executable program, which is started by java -jar, then we need to tell springboot what the current profile (environment) is when starting, namely:
java -jar xxx.jar --spring.profiles.active=test
Through this --spring.profiles.active=test is to tell springboot that the profile we set is test.
Multi-environment configuration methods commonly used in actual development
Multi-environment configuration
If you set --spring.profiles.active=test, then springboot will support loading the application-test.properties file. If we store configuration files of different environments by sub-directories, then at this time, we need to get the profile first.
How to get the current profile
Get the profile from the entry class
java -jar xxx.jar --spring.profiles.active=test
To put it plainly, the string "--spring.profiles.active=test" will be passed to the String[] args of the main method of the entry class!
CommandUtils
how to get profile from args
We traverse from the args. If we find the profile, we will set the system key/value through System.setProperty (the key here is "profile"). This is to save the profile for later access.
Note that if profile is not given, it is automatically set to dev.
Take a look at the specific analysis profile method in CommandUtils
parseSpringProfile
Env enumeration type defined
At this point, we can load the configuration file under the profile according to the profile, such as:
Refer to the profile variable set by the system
In the above, we have set System.setProperty and set the current environment information to the profile variable, so we directly referenced ${profile}.
Let's take a look at the contents of the application.properties file:
Set different startup ports according to different profiles
Suppose we want to deploy the application in the test environment:
profile=test
port=7072
verification:
Can the profile be obtained, and can the property files in a specific directory be loaded
Configuration files in different environment directories
result:
Get profile verification
Got the mongo configuration in the test environment
Okay, here, the multi-environment configuration will be introduced to everyone~
swagger
Swagger, translated in English, is a bit dragging, cool, and worthy of its name!
The integration of Swagger with springboot is very simple. It is a very powerful api framework. It not only provides access to online documents (like java doc), but also provides online http tests (like postman, etc.), simple and handsome~
How to integrate it?