02-springboot配置文件详解

springboot采纳了建立生产就绪Spring应用程序的观点。 Spring Boot优先于配置的惯例,旨在让您尽快启动和运行。在一般情况下,我们不需要做太多的配置就能够让spring boot正常运行。在一些特殊的情况下,我们需要做修改一些配置,或者需要有自己的配置属性。

自定义属性

当我们创建一个springboot项目的时候,系统默认会为我们在src/main/java/resources目录下创建一个application.properties。个人习惯,我会将application.properties改为application.yml文件,两种文件格式都支持。

在application.yml文件中定义一直属性:

my:
 name: user1
 gender: female

在代码中读取配置文件的值,只需要在字段添加@Value("${配置key}")注解,如下:

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

@RequestMapping("/testConfig")
public String testConfig(){
    return this.name+":"+this.gender;
}

启动服务后,在浏览器输入访问URL:http://localhost:8080/testConfig,显示结果如下:

user1:female

将配置文件的属性赋给实体类

当有很多配置的时候,可以将这些配置注入到一个配置Bean中,例如:
application.yml:

my:
 name: user1
 gender: female
 number: ${random.int}
 uuid: ${random.uuid}
 max: ${random.int(20)}
 value: ${random.long}
 greeting: hi,i am ${my.name}

属性配置类:

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

    private String name;
    private String gender;
    private int number;
    private String uuid;
    private int max;
    private long value;
    private String greeting;

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public long getValue() {
        return value;
    }

    public void setValue(long value) {
        this.value = value;
    }

    public String getGreeting() {
        return greeting;
    }

    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }

    @Override
    public String toString() {
        return "ConfigBean{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", number=" + number +
                ", uuid='" + uuid + '\'' +
                ", max=" + max +
                ", value=" + value +
                ", greeting='" + greeting + '\'' +
                '}';
    }
}

controller

@RestController
@EnableConfigurationProperties(ConfigBean.class)
public class ConfigTestController {
    @Autowired
    ConfigBean configBean;

    @RequestMapping("/testBean")
    public String test(){
        return this.configBean.toString();
    }
}

启动服务后,访问URL:http://localhost:8080/testBean,返回如下:

ConfigBean{name='user1', gender='female', number=1233673592, uuid='be38abf1-e8be-42ef-bb48-53a5df442013', max=19, value=-124757665702692320, greeting='hi,i am user1'}

自定义配置文件

可以不将所有的配置定义到application.yml中,可以将配置放置在properties文件中,例如application-dev.properties文件内容如下:

user.username=dev
user.gender=male
user.age=100

添加一个配置Bean:

@Configuration
@PropertySource(value="classpath:config/application-dev.properties")
@ConfigurationProperties(prefix = "user")
public class UserBean {
   private String username;
   private String gender;
   private int age;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "UserBean{" +
                "username='" + username + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                '}';
    }
}

controller:

@RestController
@EnableConfigurationProperties(ConfigBean.class)
public class ConfigTestController {
    @Autowired
    ConfigBean configBean;
    @Autowired
    UserBean userBean;

    @RequestMapping("/testBean")
    public String test(){
        return this.configBean.toString();
    }

    @RequestMapping("/userBean")
    public String user(){
        return this.userBean.toString();
    }
}

调用URL,返回结果如下:

UserBean{username='dev', gender='male', age=100}

多个环境配置文件

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

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

只需要我们在application.yml中加:

spring:
  profiles:
    active: dev

则会调用读取对应的文件。

来源:http://blog.csdn.net/forezp/article/details/70437576

你可能感兴趣的