Specific practices:
- Configurations for different environments set a profile, for example: The configuration configuration in the dev environment is in application-dev.properties, and the configuration in the PROD environment is configured in Application-prod.properties.
- Specify which file to use in Application.properties
1. Application-dev.properties(configuration under dev environment)
Profile = Dev_envrimont
2, Application-prod.properties(configuration under the PROD environment)
Profile = Prod_envrimont
3, Application.properties
1 Spring.data.mongodb.uri=mongodb://192.168.22.110:27017/myfirstmongodb2 3 # Spring.profiles.active4 Spring.profiles.active=dev
Description: The above configuration indicates the use of the configuration under the dev environment.
Note:spring.data.mongodb.uri=mongodb://192.168.22.110:27017/ Myfirstmongodb the configuration is application-dev.properties not configured
4. Controller
@Autowired Private Environment env; @RequestMapping ("/testprofile") public String testprofile () { return env.getproperty ("Profile"); }
Test :
- The result of the above code execution is: Dev_envrimont and Mongodb://192.168.22.110:27017/myfirstmongodb
- If the application.properties configuration changes to: Spring.profiles.active=prod, the result is: Prod_envrimont
- If the configuration of application.properties is changed to: Spring.profiles.active=prod, profile= is also configured in Application.properties XXX (regardless of whether the configuration is above or below the Spring.profiles.active=prod), this time the result is: Prod_envrimont
- If the configuration of application.properties is changed to: Spring.profiles.active=prod, profile= is also configured in Application.properties XXX (regardless of whether the configuration is above or below Spring.profiles.active=prod), but application-prod.properties deleted the profile = Prod_envrimont, this time the result is: XXX
Conclusion:
- The public configuration of each environment is written in application.properties
- Each module's unique configuration is configured in its own application-{xxx}.properties file
- When the program reads, first read the configuration of the selected profile in Application.properties, if not read, it will be read from Application.properties.
14th Chapter Springboot + profile (different environments read different configurations)