1.5.x之前的版本
在此之前的版本中,spring-boot的locations属性可以正常使用,即可以使用其指定配置文件的路径。
配置类需要与配置文件中的属性相同:我们可以使用如下配置类接收配置文件中的数据。
/**
* 加载yaml配置文件的方法,生成
* spring-boot更新到1.5.2版本后locations属性无法使用
* @PropertySource注解只可以加载proprties文件,无法加载yml文件
* 需要把数据放到application.yml文件中,spring-boot启动时会加载
*/
@Component
@ConfigurationProperties(locations = {"classpath:conf/serviceinfo.yml"},prefix = "test")
public class Config {
private String servicename;
private String version;
private String url;
private String protocol;
private String visualRange;
private String namespace;
private String networkPlaneType;
private String lb_policy;
private String publish_port;
private List<Map<String, String>> nodes = new ArrayList<>();
private List<Map<String, String>> metadata = new ArrayList<>();
private List<String> labels = new ArrayList<>();
private String host;
private String path;
/*set、get方法省略*/
}
注意这里的变量名称与配置文件中的名称保持一致,另外如果配置中使用max-idle类似的变量,则在java类中变量命名为maxIdle即可。
在需要读取配置文件的地方将此类注入即可:
@Configuration
public class ServiceInfoConf {
@Autowired
private Config config;
@Bean
public MicroServiceInfo microServiceInfo(){
MicroServiceInfo info = new MicroServiceInfo();
if (config != null) {
/*使用配置文件中的属性即可*/
}else{
System.out.println("SMS配置读取有误!!!");
}
return info;
}
}
此次主要运用了ConfigurationProperties中的locations属性,该配置文件支持自定义的yml配置文件(已验证),properties应该也是支持的(未验证)。
serviceinfo.yml相关配置如下:
test:
servicename: aaa
version: v1
url:
protocol: REST
visualRange: 0
namespace:
networkPlaneType:
lb_policy:
publish_port:
nodes:
- ip:
port: 8080
ttl: -1
lb_server_params:
checkType: HTTP
checkUrl:
checkInterval: 10s
checkTimeOut: 9s
ha_role: active
metadata:
- key:
value:
- key:
value:
labels:
-
host:
path:
1.5.x之后的版本
新版本中ConfigurationProperties注解中已将locations属性废弃掉,因此需要使用@PropertySource注解来指定配置文件的路径,但是有一点要注意的是:该中方法仅支持properties类型的文件,不再支持yml配置。
@Component
//@ConfigurationProperties(locations = {"classpath:conf/sms-serviceinfo.yml"},prefix = "sms")
@ConfigurationProperties(prefix = "test",ignoreUnknownFields = false)
@PropertySource("classpath:conf/serviceinfo.properties")
public class Config {
}
此时的配置文件也仅支持properties类型的
test.servicename=
test.version=v1
test.url=/
test.protocol=REST
test.visualRange=0
test.namespace=
test.networkPlaneType=
test.lb_policy=
test.publish_port=
test.nodes[0].ip=
test.nodes[0].port=8080
test.nodes[0].ttl=-1
test.nodes[0].lb_server_params=
test.nodes[0].checkType=
test.nodes[0].checkUrl=
test.nodes[0].checkInterval=
test.nodes[0].checkTimeOut=
test.nodes[0].ha_role=
test.metadata[0].aaa1=
test.metadata[1].aaa2=
test.labels[0]=
test.labels[1]=
test.host=
test.path=
注意这里的几种配置的写法:
String的可以直接配置:test.string=value (类中直接用string去接收value)
Map的直接配置为 sms.map.key=value (类中直接用map去接收Map<key,value>)
List的可以配置为 sms.list[0]=value (类中直接用list去接收List)
List
注入list和map
这里将@Value和自动装配配置的方法结合在一起使用。注意prefix使用时,配置类的属性必须与yml中的配置相同
@Configuration
@RefreshScope // 配置动态刷新时使用
@ConfigurationProperties(prefix = "msb.cors",ignoreUnknownFields = false)
@Data
public class CorsInfoProperty {
@Value("${test.cors.path:/**}")
private String pathPatternl;
private List<String> origin = new ArrayList<>();
private Map<String,String> map = new HashMap<>();
@Value("${test.cors.methods:GET,POST,OPTIONS,DELETE}")
private String methods;
@Value("${test.cors.maxage:1800}")
private long maxAge;
}
如下配置:
msb:
cors:
origin:
- 'http://10.40.159.40:8081'
- 'http://10.40.159.41:8080'
map:
key: value