14th Chapter Springboot + profile (different environments read different configurations)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.