() Spring Boot configuration

Source: Internet
Author: User

1. Core configuration of Spring boot

After the spring boot project is established, the Application.properties configuration file has been created

In fact, the configuration file also supports *.YML format;

2. configuration file for multi-configuration environment (actual development)

Application-dev.properties

Application-test.properties

Application-online.properties

Multi-environment configuration file, at this time we need to configure the application.properties, to activate one of the configuration files, the configuration is as follows:

Spring.profiles.active=dev (development environment)

So the problem is, if the development environment is configured with Port 8089, the application.properties is also configured with 8080, and finally the port that is in effect for development environment 8089

3. Spring Boot Custom configuration file

Configure the configuration you want in application.properties, then use the @value annotation in Cotnroller to get the value of the custom configuration

And get it in the controller.

Another way to read a custom configuration

Define a class, then read the value into the properties of the class, and then call the class's properties to get the configuration

/*** Define values in custom attributes*/@Component @configurationproperties (prefix= "Boot") Public classConfiginfo {PrivateString Country; Private intAge ;  PublicString getcountry () {returnCountry; }     Public voidSetcountry (String country) { This. Country =Country; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }}
@Controller Public classConfigcontroller {@Value ("${boot.country}")    PrivateString Country; @RequestMapping ("/boot/config")     Public@ResponseBody String config () {returnCountry; } @AutowiredPrivateConfiginfo Configinfo; @RequestMapping ("/boot/config2")     Public@ResponseBody String Config2 () {returnConfiginfo.getcountry () +Configinfo.getage (); }    }

() Spring Boot configuration

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.