从yml配置文件中获取属性

2018-04-15  本文已影响1738人  simoscode

1前言

从配置文件中读取配置信息,提高了代码的灵活性,可扩展性,稳定性.这篇主要讲讲几种从配置文件获取配置信息的2种方式.
源码传送门:yml获取配置信息

方式一:@Value

基本类型属性注入,直接在字段上添加@Value("${xxx.xxx}")即可.注意这里用的是$,而不是#.@Value注入的属性,一般其他属性没有关联关系.

@RestController
public class ConfigPropertiesController {
@Value("${simos.name}")
private String name;
@Value("${simos.age}")
private int age;
@Value("${simos.bodyWeight}")
private double bodyWeight;
@Value("${simos.girlFriend.beautiful}")
private Boolean beautiful;
@RequestMapping(value = "/config",method = RequestMethod.GET)
public String getConfigProperties(){
    return "{name:"+name+",age:"+age+",beautiful:"+beautiful+",bodyWeight:"+bodyWeight+"}";
}
}

方式二:@ConfigurationProperties

简单的属性注入,使用@Value即可.但是结构层次比较多,且这些结构是有关联关系的时候,就不宜使用@Value.比如springboot中server的配置:

image.png
从图中可以看出,Ssl的配置是一个实体里类,里面包含了几种属性.这样配置文件中的结构与实体类就完全一致,更容易理解.simos给自己配置了一个女朋友,三个好基友,三个体育爱好.如下
image.png

配置类如下:

/**
 * Created by l2h on 18-4-15.
 * Desc: 配置文件信息
 * @author l2h
 */
@ConfigurationProperties(prefix = "simos")
public class SimosProperties {
/**
 * 姓名
 */
private String name;
/**
 * 性别
 */
private String sex;
/**
 * 体重
 */
private Double bodyWeight;
/**
 * 年龄
 */
private int age;
/**
 * 女票
 */
private  GirlFriend girlFriend = new GirlFriend();
/**
 * 基友
 */
private List<Friend> friends = new ArrayList<>();
public String getName() {
    return name;
}

public String getSex() {
    return sex;
}

public Double getBodyWeight() {
    return bodyWeight;
}

public int getAge() {
    return age;
}

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

public void setSex(String sex) {
    this.sex = sex;
}

public void setBodyWeight(Double bodyWeight) {
    this.bodyWeight = bodyWeight;
}

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

public GirlFriend getGirlFriend(){
    return this.girlFriend;
}
public List<Friend> getFriends(){
    return this.friends;
}
}

配置属性文件对应的,@Configuration如下:

/**
 * Created by l2h on 18-4-15.
 * Desc:
 */
@Configuration
@EnableConfigurationProperties(SimosProperties.class)
public class SimosConfiguration {

private final SimosProperties simosProperties;
public SimosConfiguration(SimosProperties simosProperties){
    this.simosProperties = simosProperties;
}
public SimosProperties getSimosProperties(){
    return simosProperties;
}
}

通过注解@EnableConfigurationProperties,处理后会生成一个SimosProperties bean.这样就可以
在其他需要的地方注入了:

/**
 * Created by l2h on 18-4-15.
 * Desc:
 */
@RestController
public class ConfigPropertiesController {
@Autowired
SimosProperties properties;
@Value("${simos.name}")
private String name;
@Value("${simos.age}")
private int age;
@Value("${simos.bodyWeight}")
private double bodyWeight;
@Value("${simos.girlFriend.beautiful}")
private Boolean beautiful;
@RequestMapping(value = "/config",method = RequestMethod.GET)
public String getConfigProperties(){
    return "{name:"+name+",age:"+age+",beautiful:"+beautiful+",bodyWeight:"+bodyWeight+"}";
}
@RequestMapping(value = "/simos",method = RequestMethod.GET)
public SimosProperties getSimosProperties(){
    return properties ;
}
}

这里需要注意的一点是,如果不通过@EnableConfigurationProperties方式,SimosProperties是不会生成bean的,不信的朋友可以试试.当然啦,也可以直接在SimosProperties上添加注解@Component,这样它即是properties类,也是普通的bean.

上一篇 下一篇

猜你喜欢

热点阅读