properties使用PropertiesLoaderUtils,yml使用YamlPropertySourceLoader
application.properties
microsoft.definition.schema=https://websiate/....
microsoft.definition.action.input.NOTIFICATION.uri=https://websiate/....
microsoft.definition.action.input.DEVICE_CMD.uri=https://websiate/....
java读取的代码
String uri = "microsoft.definition.action.input." + actionsType + ".uri";
Properties properties = PropertiesLoaderUtils.loadAllProperties("application.yml"); String ActionsInputUri = properties.getProperty(uri); definiteActionUriMap.put(actionsType, ActionsInputUri)
yml读取的版本,且区分环境
application.yml
spring:
application:
name: codeconvert
profiles:
active: dev
application-dev.yml
microsoft:
definition:
schema: https://website/..
action:
input:
NOTIFICATION:
uri: https://website/..
DEVICE_CMD:
uri: https://website/..
application-test.yml
microsoft:
definition:
schema: https://website/test
action:
input:
NOTIFICATION:
uri: https://website/test
DEVICE_CMD:
uri: https://website/test
@Value("${spring.profiles.active}") public String active; //具体执行代码 String uri = "microsoft.definition.action.input." + actionsType + ".uri"; Resource activeresource=new ClassPathResource(active==null?("application.yml"):"application-"+active+".yml"); List<PropertySource<?>> load = new YamlPropertySourceLoader().load(uri, activeresource); String ActionsInputUri =""; if(load!=null&&!load.isEmpty()){ ActionsInputUri = load.get(0).getProperty(uri)==null?"":load.get(0).getProperty(uri).toString(); definiteActionUriMap.put(actionsType, ActionsInputUri); }