"Springboot series" II: Springboot Configuration Detailed

Source: Internet
Author: User


With the introduction to spring boot from the previous blog, you also saw that spring boot is not really a so-called "0 configuration", and his philosophy is that "custom is better than configuration" with some default habitual configuration that lets you run your project quickly without having to configure it manually. So if you want to play with spring Boot, it's still essential to understand these default configurations.


When you create a spring boot project, a global profile application.properties (which can be modified with a suffix of. yml) is generated by default, under the Src/main/resources directory or under the/config of the classpath. We can modify configuration values for some of the default configurations by modifying the configuration file. Custom Properties


We can configure some constants or other parameter configurations in the Application.yml file. Read by the Spring @value ("${Property Name}") annotations.

define several constants in APPLICATION.YML:

ip:111.111.111.111
port:8080

read the configuration on the controller:

/**
 * Created by Qianqian.niu on 2017/6/27.
 *
/@RestController public
class Helloconfigcontroller {

    @Value ("${ip}")
    private String IP;

    @Value ("${port}")
    private String port;

    @GetMapping ("/gethost") public
    String GetHost () {
        return "IP:" + IP + "PORT:" + port;
    }

}

Access Http://localhost:8080/getHost display:;

ip:111.111.111.111 port:8080


entity class attribute assignment value

When the attribute parameters become more, we are accustomed to creating an entity that uses entities to uniformly receive the assigned values.

To define a configuration file:

Userbody:
  name:erniuxxx
  password:888
  birthday:1992.10.28
  mobile:133xxxx
  Address: Chaoyang District, Beijing

To create an entity class:

/** * Created by Qianqian.niu on 2017/6/27.
    */@ConfigurationProperties (prefix = "Userbody") public class Userbody {private String name;
    private String password;
    Private String birthday;
    Private String Mobile;

    Private String address;
    Public String GetName () {return name;
    } public void SetName (String name) {this.name = name;
    } public String GetPassword () {return password;
    } public void SetPassword (String password) {this.password = password;
    } public String Getbirthday () {return birthday;
    } public void Setbirthday (String birthday) {this.birthday = birthday;
    } public String Getmobile () {return mobile;
    public void Setmobile (String mobile) {this.mobile = mobile;
    } public String getaddress () {return address;
    The public void setaddress (String address) {this.address = address; } @Override
    Public String toString () {return "userbody{" + "Name= '" + name + ' \ ' + ", p Assword= ' + password + ' \ ' + ', birthday= ' + birthday + ' \ ' + ', mobile= ' + mobile +
    ' \ ' + ', address= ' + address + ' \ ' + '} '; }
}

You need to add the annotation @configurationproperties on the entity class and specify the Prrfix prefix.

Controller invocation:

/**
 * Created by Qianqian.niu on 2017/6/27.
 *
/@RestController @EnableConfigurationProperties ({userbody.class}) public
class Hellocontroller {

    @Autowired
    Userbody Userbody;

    @GetMapping ("/getuser") public
    String GetUser () {
        return userbody.tostring ();
    }

}

The enableconfigurationproperties annotation needs to be added to the calling class, or it can be added on the startup class Springbootsimpleapplication.

Call Http://localhost:8080/getUser

Userbody{name= ' erniuxxx ', password= ' 888 ', birthday= ' 1992.10.28 ', mobile= ' 133xxxx ', address= ' Chaoyang District, Beijing '}

In addition, a small problem is found if the configuration defines the use of "user"

User:
  name:erniuxxx
  password:888

Then get the name will be the computer names, good magic.

Userbody{name= ' Erniu ', password= ' 888 ', birthday= ' 1992.10.28 ', mobile= ' 133xxxx ', address= ' Chaoyang District, Beijing '}

Also, each parameter in the APPLICATION.YML can be referenced directly to each other:

User:
  name:erniuxxx
  password:888
  Address: Chaoyang District, Beijing
  remark: ${address}-${name}


Custom configuration Files

APPLICATION.YML is the default configuration file for the system, and of course we can create a custom configuration file that creates a file under path src/main/resources test.properties

Ps:spring Boot 1.5 version @propertysource annotations cannot load custom YML profiles

define Test.properties:

Testuser.name = "songxiao222"
Testuser.password = "123"
testuser.birthday = "1992.10.28"

assign the configuration to JavaBean:

/** * Created by Qianqian.niu on 2017/6/27. */@Configuration//@PropertySource ("Classpath:test1.yml") @PropertySource ("Classpath:test.properties") @
    Configurationproperties (prefix = "TestUser") public class TestUser {private String name;
    private String password;

    Private String birthday;
    Public String GetName () {return name;
    } public void SetName (String name) {this.name = name;
    } public String GetPassword () {return password;
    } public void SetPassword (String password) {this.password = password;
    } public String Getbirthday () {return birthday;
    } public void Setbirthday (String birthday) {this.birthday = birthday;
                } @Override Public String toString () {return ' userbody{' + ' name= ' "+ name + ' \ ' +
    ", password= '" + password + "\" + ", birthday= ' + birthday + ' \ ' + '} '; }
}

1. @Configuration Annotations contain @component annotations
2. Spring Boot If it is a 1.5 previous version, you can specify the location of the properties file via locations:

@ConfigurationProperties (prefix = "TestUser", locations= "Classpath:test.properties")

3. After version 1.5, you need to specify the configuration file via @propertysource ("Classpath:test.properties")

Controller Read configuration:

/**
 * Created by Qianqian.niu on 2017/6/27.
 *
/@RestController @EnableConfigurationProperties ({testuser.class,userbody.class}) public
class Helloconfigcontroller {

    @Autowired
    TestUser TestUser;

    @Autowired
    userbody userbody;

    @GetMapping ("/gettestuser") public
    String Gettestuser () {
        return testuser.tostring ();
    }

    @GetMapping ("/getuser") public
    String GetUser () {
        return userbody.tostring ();
    }

}


Multiple Environment Profiles

About the multi-environment configuration file, previously wrote a blog: http://blog.csdn.net/u010028869/article/details/50818710
Use the Maven –p command to specify which configuration file is used when packaging.

This is a very simple approach that spring Boot offers.

The multi-Environment profile file name in spring boot needs to meet the application-{profile}.properties format, where {profile} corresponds to your environment ID, such as:

Application-test.properties: Test Environment
Server:
  port:8083

application-dev.properties: Development environment
Server:
  port:8081

application-prod.properties: Production environment
Server:
  port:8082

To use the corresponding environment, you only need to use the Spring.profiles.active property in Application.yml to set the value is {profile}, such as Dev, prod.

Spring:
  Profiles:
    Active:dev

Start the project and discover that the port of the program is no longer 8080, but 8081

Of course, you can also specify which configuration file to use when you start the jar package with the jar command:

Java-jar Xxx.jar--spring.profiles.active=dev


Summary

Spring boot configuration is quite simple, and I hope that the information about the configuration can help you. Here also buried a small pit, is how to load the custom yml file, the next time when the spring boot custom listener to solve the problem ~

Source code Download: fill in later

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.