【SpringBoot系列】二:SpringBoot配置详解

来源:互联网 发布:highcharts more.js 编辑:程序博客网 时间:2024/04/30 02:58


通过上一篇博客对Spring Boot的介绍,你也看见了Spring Boot并不真正是所谓的『零配置』,他的理念是“习惯优于配置”采用了一些默认的习惯性配置,让你无需手动进行配置,从而让你的项目快速运行起来。所以要想玩转Spring Boot,了解这些默认配置还是必不可少的。


创建Spring Boot项目时,会默认生成一个全局配置文件application.properties(可以修改后缀为.yml),在src/main/resources目录下或者类路径的/config下。我们可以通过修改该配置文件来对一些默认配置的配置值进行修改。

自定义属性


我们可以在application.yml文件中,配置一些常量或者其他参数配置。读取的时候通过Spring的@Value(“${属性名}”)注解即可。

在application.yml定义几个常量:

ip: 111.111.111.111port: 8080

在Controller读取该配置:

/** * Created by qianqian.niu on 2017/6/27. */@RestControllerpublic class HelloConfigController {    @Value("${ip}")    private String ip;    @Value("${port}")    private String port;    @GetMapping("/getHost")    public String getHost() {        return "ip:" + ip + " port:" + port;    }}

访问 http://localhost:8080/getHost 显示:;

ip:111.111.111.111 port:8080


实体类属性赋值

当属性参数变多的时候,我们习惯创建一个实体,用实体来统一接收赋值这些属性。

定义配置文件:

UserBody:  name: erniuxxx  password: 888  birthday: 1992.10.28  mobile: 133xxxx  address: 北京市朝阳区

创建实体类:

/** * 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;    }    public void setAddress(String address) {        this.address = address;    }    @Override    public String toString() {        return "UserBody{" +                "name='" + name + '\'' +                ", password='" + password + '\'' +                ", birthday='" + birthday + '\'' +                ", mobile='" + mobile + '\'' +                ", address='" + address + '\'' +                '}';    }}

需要在实体类上增加注解@ConfigurationProperties,并指定prrfix前缀。

controller调用:

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

EnableConfigurationProperties注解需要加在调用类上,或者加在启动类SpringbootSimpleApplication上也可以。

调用 http://localhost:8080/getUser

UserBody{name='erniuxxx', password='888', birthday='1992.10.28', mobile='133xxxx', address='北京市朝阳区'}

另外发现一个小问题,如果配置中定义使用”user”

user:  name: erniuxxx  password: 888

那么获取的name将会是电脑的名称,好神奇。

UserBody{name='erniu', password='888', birthday='1992.10.28', mobile='133xxxx', address='北京市朝阳区'}

还有,在application.yml中的各个参数之间也可以直接互相引用:

user:  name: erniuxxx  password: 888  address: 北京市朝阳区  remark: ${address}-${name}


自定义配置文件

application.yml是系统默认的配置文件,当然我们也可以创建自定义配置文件,在路径src/main/resources下面创建文件test.properties

PS:spring boot 1.5版本后@PropertySource注解就不能加载自定义的yml配置文件了

定义test.properties:

TestUser.name = "songxiao222"TestUser.password = "123"TestUser.birthday = "1992.10.28"

将配置赋值到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 注解包含@Component注解
2. spring boot如果是1.5以前的版本,那么可以通过locations指定properties文件的位置:

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

3. 1.5版本后需要通过@PropertySource(“classpath:test.properties”)指定配置文件

Controller 读取配置:

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


多个环境配置文件

关于多环境配置文件,之前写过一篇博客:http://blog.csdn.net/u010028869/article/details/50818710
通过maven的 –P命令来指定打包时的使用那个配置文件。

相比而言Spring Boot 提供的这种方式真是太简单了

在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:

application-test.properties:测试环境server:  port: 8083application-dev.properties:开发环境server:  port: 8081application-prod.properties:生产环境server:  port: 8082

想要使用对应的环境,只需要在application.yml中使用spring.profiles.active属性来设置,值就是{profile},如dev、prod。

spring:  profiles:    active: dev

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

图片名称

当然用jar命令启动jar包的时候也可以指定使用哪个配置文件:

java -jar xxx.jar --spring.profiles.active=dev


小结

Spring Boot的配置还是相当简便的,希望在此整理的有关配置的信息能够帮到大家。这里也埋了个小坑,就是如何加载自定义的yml文件,下次讲到Spring Boot自定义监听器的时候在解决这个问题~

源码下载:稍后补上