服务器route配置文件,spring-boot-route(二)读取配置文件的几种方式

dc25a6aed567a1f78fbd189afc43525a.png

spring-boot-route(二)读取配置文件的几种方式

76697f43b6dadf5352964015d0aea05b.png

Spring Boot提供了两种格式的配置文件,分别是properties 和 yml。Spring Boot最大的特点就是自动化配置,如果我们想修改自动化配置的默认值,就可以通过配置文件来指定自己服务器相关的参数。

配置文件集约管理了配置信息,如果把配置参数写到Java代码中,维护起来非常不方便,如果使用配置文件,我们可以统一管理,统一修改。我比较推荐使用yml格式的配置文件,YAML是专门用来写配置文件的语言,通常以yml为后缀,它的结构非常清晰,更易于阅读。

将自定义的配置写在配置文件中后,如果想要在java代码中使用配置,这时候就需要读取配置文件,读取配置文件的方式有三种,我们挨个介绍一下如果进行读取!

第一种:使用@Value注解读取

第一步:在配置文件中增加加入以下配置

config:

name: Java旅途

desc: spring-boot-route

第二部:新建Java类读取配置信息

@RestController

public class GetValue{

@Value("${config.name}")

private String name;

@Value("${config.desc}")

private String desc;

@GetMapping("getValue")

public String getValue(){

return "name="+name+";desc="+desc;

}

}

@Value注解使用简单,适合单个参数的注入。

第二种:使用@ConfigurationProperties读取

@ConfigurationProperties与@Value相比,更加适合读取数组类型的参数。

1. 获取单个对象

第一步:在yml文件中新建对象类型的配置信息

configs:

config:

name: Java旅途

desc: spring-boot-route

第二步:新建实体映射配置信息

@Component

@ConfigurationProperties(prefix = "configs.config")

@Data

public class Config{

private String name;

private String desc;

}

第三步:新建类测试是否获取到参数

@RestController

public class GetConfigurationProperties{

@Autowired

private Config config;

@GetMapping("/getConfig")

public String getConfig(){

return config.getName()+";"+config.getDesc();

}

}

2. 获取对象集合

第一步:在yml文件中新建数组类型的参数

configs:

config:

- name: Java旅途

desc: spring-boot-route

- name: javatrip

desc: spring-boot-yml

第二步:新建实体映射配置信息

@Component

@ConfigurationProperties(prefix = "configarr")

@Data

public class Configs{

private List config = new ArrayList<>();

@Data

public static class Config{

private String name;

private String desc;

}

}

第三步:新建测试类获取参数

@RestController

public class GetConfigurationProperties{

@Autowired

private Configs configs;

@GetMapping("/getConfigs")

public String getConfigs(){

String content = "";

List configList = configs.getConfig();

Map map = new HashMap<>();

for (Configs.Config bean : configList){

content += bean.getName()+";"+bean.getDesc()+",";

}

return content;

}

}

除了上面介绍的两种方式之外,还可以通过Spring Boot上下文的环境变量来读取配置文件信息,不过上面两种方式已经完全可以满足所有需求,这里就不再进行介绍了。

思考与扩展

如果多个配置文件具有相同的配置信息,那么如何读取特定的配置文件信息呢?

配置文件具有优先级,一般情况下,yml文件的优先级高于properties,这样就会导致properties的配置信息后加载,最后读取的时候就会properties的配置信息的优先级会更高。

上面介绍的两种读取配置文件的方式可以和另一个注解配合使用,@PropertySource常用的三个属性,一个是value用于指定配置文件,另一个是encoding用于指定编码,最后一个是factory,用于指定解析工厂。

这里需要注意一下:@PropertySource默认只会加载properties格式的文件,也就是我们如果指定了yml类型的文件是不会生效的,这时候就需要我们重写解析工厂。

先看看下默认的解析工厂源码:

public class DefaultPropertySourceFactory implements PropertySourceFactory{

public DefaultPropertySourceFactory(){

}

public PropertySource> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {

return name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource);

}

}

自定义解析工厂,实现PropertySourceFactory

public class YmlConfigFactory extends DefaultPropertySourceFactory{

@Override

public PropertySource> createPropertySource(String name, EncodedResource resource) throws IOException {

String sourceName = name != null ? name : resource.getResource().getFilename();

if (!resource.getResource().exists()) {

return new PropertiesPropertySource(sourceName, new Properties());

} else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {

Properties propertiesFromYaml = loadYml(resource);

return new PropertiesPropertySource(sourceName, propertiesFromYaml);

} else {

return super.createPropertySource(name, resource);

}

}

private Properties loadYml(EncodedResource resource) throws IOException{

YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();

factory.setResources(resource.getResource());

factory.afterPropertiesSet();

return factory.getObject();

}

}

第一步:新建两个配置文件,test.yml和test.properties,增加以下配置信息

spring:

value: javatrip123

remark: javatrip123

spring:

value: javatrip123

remark: javatrip123

第二步:指定配置文件映射配置文件内容

@Data

@Configuration

@PropertySource(value = {"classpath:test.yml"},encoding = "gbk")

@ConfigurationProperties(prefix = "spring")

public class Spring{

private String value;

private String remark;

}

第三步:新建类进行测试

@RestController

public class GetSource{

@Autowired

private Spring spring;

@GetMapping("get")

public String getSource(){

return spring.getRemark()+";"+spring.getValue();

}

}

此是spring-boot-route系列的第二篇文章,这个系列的文章都比较简单,主要目的就是为了帮助初次接触Spring Boot 的同学有一个系统的认识。本文已收录至我的github,欢迎各位小伙伴star!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值