Skip to content

Instantly share code, notes, and snippets.

@yangl
Last active July 12, 2019 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yangl/2793e02fcd4b9fd2848a145ff5bf85f0 to your computer and use it in GitHub Desktop.
Save yangl/2793e02fcd4b9fd2848a145ff5bf85f0 to your computer and use it in GitHub Desktop.
spring-boot2 读取自定义yml文件 <code>YAML files cannot be loaded by using the @propertysource annotation. </code> 详见:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-yaml-shortcomings
package com.vip.saturn.job.console.config;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import java.io.IOException;
import java.util.List;
/**
*
* YAML属性配置工厂类
*
* @see org.springframework.core.io.support.DefaultPropertySourceFactory
*
* 默认只读取properties、xml配置
*
* @author YANGLiiN
*
*/
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
List<PropertySource<?>> propertySources = yamlPropertySourceLoader.load(name, resource.getResource());
if (propertySources != null && !propertySources.isEmpty()) {
return propertySources.get(0);
}
return null;
}
}
package com.vip.saturn.job.console.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.util.List;
import java.util.Map;
@Configuration
@PropertySource(value = {"classpath:/alertnotice.yml"}, factory = YamlPropertySourceFactory.class, encoding = "UTF-8")
@ConfigurationProperties(prefix = "alert")
public class UNPConfig {
// 统一通知平台服务 URL
private static final String NAME_UNP_SERVICE_URL = "UNP_SERVICE_URL";
public static final String UNP_SERVICE_URL = System
.getProperty(NAME_UNP_SERVICE_URL, System.getenv(NAME_UNP_SERVICE_URL));
// 统一通知平台服务 ACCESS_KEY
private static final String NAME_UNP_ACCESS_KEY = "UNP_ACCESS_KEY";
public static final String UNP_ACCESS_KEY = System
.getProperty(NAME_UNP_ACCESS_KEY, System.getenv(NAME_UNP_ACCESS_KEY));
// 统一通知平台服务 ACCESS_TOKEN
private static final String NAME_UNP_ACCESS_TOKEN = "UNP_ACCESS_TOKEN";
public static final String UNP_ACCESS_TOKEN = System
.getProperty(NAME_UNP_ACCESS_TOKEN, System.getenv(NAME_UNP_ACCESS_TOKEN));
// 告警短信 namespace 手机号
private Map<String, List<String>> sms;
// 告警短信管理员手机号
private List<String> admins;
public Map<String, List<String>> getSms() {
return sms;
}
public void setSms(Map<String, List<String>> sms) {
this.sms = sms;
}
public List<String> getAdmins() {
return admins;
}
public void setAdmins(List<String> admins) {
this.admins = admins;
}
}
alert:
admins:
- 181239xxxxx
- 158765xxxxx
sms:
public:
- 181239xxxxx
dds-pro-1-A:
- 186766xxxxx
dds-pro-1-B:
- 186766xxxxx
dds-pro-1-C:
- 186766xxxxx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment