springboot (2) SpringBoot configuration file

1. Custom properties

When creating a springboot project, the system will create an application.properties for us in the src/main/java/resources directory by default. Change application.properties to application.yml file, both file formats are supported.
Customize a set of properties in application.yml: It
should be noted that
you cannot use the TAB key to indent when editing the yml file, otherwise the startup will report an error.
A space is required between the key values. For example, enabled: true means that there is actually a space in front of true, otherwise an error will be reported.

my:
 name: 
   first: zhang
   second: san
 age: 12

If you need to read the value of the configuration file just add @Value("${property name}"):

@RestController
@RequestMapping("my") //url中的前缀
public class MiyaController {

    @Value("${my.name.first}")
    private String firstName;
    @Value("${my.age}")
    private int age;

    @RequestMapping(value = "/miya")
    public String miya(){
        return firstName+":"+age;
    }

}

Start the project, visit: localhost:8080/my/miya, the browser displays:

zhang:12

Second, assign the attributes of the configuration file to the entity class

When we have a lot of configuration properties, then we will use these properties as fields to create a javabean and assign property values ​​to them, such as:

my:
 name: forezp
 age: 12
 number:  ${random.int}
 uuid : ${random.uuid}
 max: ${random.int(10)}
 value: ${random.value}
 greeting: hi,i'm  ${my.name}

used in the configuration file r a n d O m it Can by use Come pregnancy become each kind Do not same kind type of follow machine value {my.name}: Get the value in the configuration.

To assign these properties to a javabean, first create a javabean:

@ConfigurationProperties(prefix = "my")
@Component
public class ConfigBean {

    private String name;
    private int age;
    private int number;
    private String uuid;
    private int max;
    private String value;
    private String greeting;

    省略了getter setter....

You need to add an annotation @ConfigurationProperties and add its prrfix. In addition, @Component can be added or not. In addition, the spring-boot-configuration-processor dependency can be added or not, the specific reason is unknown.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

In addition, you need to add the EnableConfigurationProperties annotation to the application class or application class (introduce the entity class).

@RestController
//引入实体类
@EnableConfigurationProperties({ConfigBean.class})
public class LucyController {
    @Autowired//自动检测引入该类
    ConfigBean configBean;

    @RequestMapping(value = "/lucy")
    public String miya(){
        return configBean.getGreeting()+" >>>>"+configBean.getName()+" >>>>"+ configBean.getUuid()+" >>>>"+configBean.getMax();
    }
}

Annotation @Autowired : It can annotate class member variables, methods and constructors to complete the work of automatic assembly. Eliminate set, get methods through the use of @Autowired. When performing automatic injection, the number of matching candidate beans in the spring container must be one and only one.

Visit localhost:8080/lucy, we will find that the configuration file information has been read.

Third, the custom configuration file

Sometimes we don't want to write the configuration into the application configuration file, then we need to customize the configuration file, such as test.properties:

com.forezp.name=forezp
com.forezp.age=12
@Configuration
@PropertySource(value = "classpath:test.properties")
@ConfigurationProperties(prefix = "com.forezp")
public class User {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

In the latest version of springboot, these three annotations need to be added. @Configuration
@PropertySource(value = "classpath:test.properties")
@ConfigurationProperties(prefix = "com.forezp");
PropertySource plus location is required in version 1.4.

@RestController
@EnableConfigurationProperties({ConfigBean.class,User.class})
public class LucyController {
    @Autowired
    ConfigBean configBean;

    @RequestMapping(value = "/lucy")
    public String miya(){
        return configBean.getGreeting()+" >>>>"+configBean.getName()+" >>>>"+ configBean.getUuid()+" >>>>"+configBean.getMax();
    }

    @Autowired
    User user;
    @RequestMapping(value = "/user")
    public String user(){
        return user.getName()+user.getAge();
    }

}

Start the project and open localhost:8080/user; the browser will display:

forezp12

Four, multiple environment configuration files

In a real development environment, we need different configuration environments; the format is application-{profile}.properties, where {profile} corresponds to your environment identifier, for example:

  • application-test.properties: test environment
  • application-dev.properties: development environment
  • application-prod.properties: Production environment

how to use? Just need us to add in application.yml:

#配置激活的配置文件为dev
spring:
  profiles:
    active: dev

where application-dev.yml:

#配置端口号
server:
  port: 8082

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325648509&siteId=291194637