SpringBoot Unofficial Tutorial | Part 2: Detailed Spring Boot Configuration File

Please indicate the source for reprinting: 
http://blog.csdn.net/forezp/article/details/70437576 
This article is from Fang Zhipeng's blog

springboot takes the perspective of building production-ready Spring applications. Spring Boot takes precedence over configuration conventions and aims to get you up and running as quickly as possible. In general, we don't need to do too much configuration to make spring boot run normally. In some special cases, we need to modify some configurations, or we need to have our own configuration properties.

1. Custom properties

When we create a springboot project, the system will create an application.properties for us in the src/main/java/resources directory by default. Personally, I will change application.properties to application.yml file, both file formats are supported.

Customize a set of properties in application.yml:

my:
 name: forezp
 age: 12

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

@RestController
public class MiyaController {

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

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

}

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

forezp: 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}

The configuration file uses ${random}, which can be used to generate various types of random values.

How 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>

另外需要在应用类或者application类,加EnableConfigurationProperties注解。


@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();
    }

启动工程,访问localhost:8080/lucy,我们会发现配置文件信息读到了。

三、自定义配置文件

上面介绍的是我们都把配置文件写到application.yml中。有时我们不愿意把配置都写到application配置文件中,这时需要我们自定义配置文件,比如test.properties:

com.forezp.name=forezp
com.forezp.age=12

怎么将这个配置文件信息赋予给一个javabean呢?

@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;
    }
}

在最新版本的springboot,需要加这三个注解。@Configuration 
@PropertySource(value = “classpath:test.properties”) 
@ConfigurationProperties(prefix = “com.forezp”);在1.4版本需要 
PropertySource加上location。

@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();
    }

}

启动工程,打开localhost:8080/user;浏览器会显示:

forezp12

四、多个环境配置文件

在现实的开发环境中,我们需要不同的配置环境;格式为application-{profile}.properties,其中{profile}对应你的环境标识,比如:

  • application-test.properties:测试环境
  • application-dev.properties:开发环境
  • application-prod.properties:生产环境

怎么使用?只需要我们在application.yml中加:

 spring:
  profiles:
    active: dev

其中application-dev.yml:

 server:
  port: 8082

启动工程,发现程序的端口不再是8080,而是8082。

源码下载:https://github.com/forezp/SpringBootLearning

五、参考文献

spring-boot-reference-guide-zh

pring Boot干货系列:(二)配置文件解析

Spring Boot属性配置文件详解

Guess you like

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