springboot 配置文件格式

springboot支持properties、yml两种文件格式,如果这两个文件同时存在,默认读取properties格式文件。

读取单个属性

在resources下创建 application.properties 或 application.yml 文件
application.properties 添加如下内容

solr.addrUrl=http://192.168.30.161:8082/solr
solr.jlCoreName=dtxxYddlPt
solr.jqCoreName=sjJcjJjxx
auth.timeout=500001
auth.appId=102
auth.systemId=extCd

application.yml添加如下内容

solr: 
 addrUrl: http://192.168.30.161:8081/solr
 jlCoreName: dtxxYddlPt
 jqCoreName: sjJcjJjxx
auth: 
 timeout: 50000
 appId: 102
 systemId: extCd

读取配置文件的字段

@RestController
public class HelloController {
    @Value("${solr.addrUrl}")
    private String addrUrl;
    @Value("${auth.timeout}")
    private String timeout;
    
    @RequestMapping("/user/getUser")
    public User getUser(){
        User user = new User();
        user.setId("1");
        user.setName("欧欧专区");
        user.setSex("女");
        Map<String,Object> obj = new HashMap<String,Object>();
        obj.put("addrUrl", addrUrl);
        obj.put("timeout", timeout);
        user.setObj(obj);
        return user;
    }

}

在浏览器上访问http://localhost:8080/user/getUser

 

 

 

多个属性配装到javabean

可以把application.yml配置文件属性自动装配到javabean上
新建application.yml内容如下:注意${}的用法

solr: 
 addrUrl: http://192.168.30.161:8081/solr
 jlCoreName: dtxxYddlPt
 jqCoreName: sjJcjJjxx
auth: 
 timeout: 50000
 uuid: ${random.uuid}
 value: ${random.value}
 max: ${random.int(10)}
 appId: 102 ${random.int}
 systemId: extCd ${solr.jlCoreName}

新建AuthConf装配类

@ConfigurationProperties(prefix="auth")
public class AuthConf {
      private long timeout;
      private String uuid;
      private String value;
      private int max;
      private String appId;
      private String systemId;
.....省略set、get方法,添加ConfigurationProperties配置

在启动类上添加 @EnableConfigurationProperties({AuthConf.class})

使用:

@RestController
public class HelloController {

    @Autowired
    private AuthConf authConf;    
    
    @RequestMapping("/user/getUser")
    public User getUser(){
        User user = new User();
        user.setId("1");
        user.setName("欧欧专区");
        user.setSex("女");
        Map<String,Object> obj = new HashMap<String,Object>();
        obj.put("authConf", authConf);
        user.setObj(obj);
        return user;
    }

}

自定义配置文件

项目肯定不止一个配置文件,如果要新建其它的配置文件应该怎样加载,这就要使用到自定义配置文件了
新建 address.properties 文件

redis.username=ouou
redis.password=123456

新建AddressConf 类

@Configuration
//@PropertySource(value="classpath:address.yml")
@PropertySource(value="classpath:address.properties")
@ConfigurationProperties(prefix="redis")
@JsonIgnoreProperties()
public class AddressConf{
    private String username;
     private String password;
     ....set get方法省略 yml文件不知道为什么无效

启动类添加注解

@EnableConfigurationProperties({AuthConf.class,AddressConf.class})

注意:经过这样封装后,AddressConf不能直接转换为 json返回前端,可能是springboot对这个类添加了很多属性造成的,经过这样封装,可以看出AddressConf多了很多属性,导致转换为json时报序列化错误

 

 

多环境配置

创建四个文件
application-dev.yml 开发环境
application-test.yml 测试环境
application-prod.yml 生产环境
application.yml 配置那个环境生效。。如图

 

 在application.yml 添加

spring: 
 profiles: 
  active: test

则测试环境的配置文件生效

bootstrap.yml(bootstrap.properties)与application.yml(application.properties)区别

bootstrap.yml(bootstrap.properties)用来在程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到参数等

application.yml(application.properties) 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。

bootstrap.yml 先于 application.yml 加载

 

posted on 2019-12-20 13:44  欧欧专栏  阅读(439)  评论(0)    收藏  举报