Spring中添加自定义的yml文件,并用value读取值
方案A:
实现EnvironmentPostProcessor接口
并使用YamlPropertySourceLoader加载文件
例子:
public class EnvironmentPostProcessorExample implements EnvironmentPostProcessor {
private final YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
Resource path = new ClassPathResource("com/example/myapp/config.yml");
PropertySource<?> propertySource = loadYaml(path);
environment.getPropertySources().addLast(propertySource);
}
private PropertySource<?> loadYaml(Resource path) {
if (!path.exists()) {
throw new IllegalArgumentException("Resource " + path + " does not exist");
}
try {
return this.loader.load("custom-resource", path).get(0);
}
catch (IOException ex) {
throw new IllegalStateException("Failed to load yaml configuration from " + path, ex);
}
}
}
优点:Spring官方例子,比较安全
缺点:需要配置META-INF/spring.factories文件,key为EnvironmentPostProcessor的全路径,value为EnvironmentPostProcessorExample全路径
方案B:
使用PropertySource注解,需要自己实现factory方法
例子:
public class YamlSourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String s, EncodedResource encodedResource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(encodedResource.getResource());
factory.afterPropertiesSet();
Properties object = factory.getObject();
return new PropertiesPropertySource(“cumstom-properties”, object);
}
}
优点:比较方便
缺点:spring官方不是很推荐,启动过程会慢一点
方案C:
config类中bean
需要返回PropertySourcesPlaceholderConfigurer
例子:
@Bean
public PropertySourcesPlaceholderConfigurer load(){
YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
MutablePropertySources sources = new MutablePropertySources();
Resource resource = new ClassPathResource(“serviceconfig/service.yml”);
Resource appResource = new ClassPathResource(“application.yml”); // 如果不写这个 application的文件就失效了
try {
PropertySource load = yamlPropertySourceLoader.load(“service”, resource, null);
PropertySource loadApp = yamlPropertySourceLoader.load(“app”, appResource, null);
sources.addLast(load);
sources.addLast(loadApp);
configurer.setPropertySources(sources);
return configurer;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
优点:网上大多数是这样写的
缺点:如果要多个yaml文件的话,需要把需要的都写在这里,这个方法会让spring默认的文件失效Spring中添加自定义的yml文件,并用value读取值
方案A:
实现EnvironmentPostProcessor接口
并使用YamlPropertySourceLoader加载文件
例子:
public class EnvironmentPostProcessorExample implements EnvironmentPostProcessor {
private final YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
Resource path = new ClassPathResource("com/example/myapp/config.yml");
PropertySource<?> propertySource = loadYaml(path);
environment.getPropertySources().addLast(propertySource);
}
private PropertySource<?> loadYaml(Resource path) {
if (!path.exists()) {
throw new IllegalArgumentException("Resource " + path + " does not exist");
}
try {
return this.loader.load("custom-resource", path).get(0);
}
catch (IOException ex) {
throw new IllegalStateException("Failed to load yaml configuration from " + path, ex);
}
}
}
优点:Spring官方例子,比较安全
缺点:需要配置META-INF/spring.factories文件,key为EnvironmentPostProcessor的全路径,value为EnvironmentPostProcessorExample全路径
方案B:
使用PropertySource注解,需要自己实现factory方法
例子:
public class YamlSourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String s, EncodedResource encodedResource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(encodedResource.getResource());
factory.afterPropertiesSet();
Properties object = factory.getObject();
return new PropertiesPropertySource(“cumstom-properties”, object);
}
}
优点:比较方便
缺点:spring官方不是很推荐,启动过程会慢一点
方案C:
config类中bean
需要返回PropertySourcesPlaceholderConfigurer
例子:
@Bean
public PropertySourcesPlaceholderConfigurer load(){
YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
MutablePropertySources sources = new MutablePropertySources();
Resource resource = new ClassPathResource(“serviceconfig/service.yml”);
Resource appResource = new ClassPathResource(“application.yml”); // 如果不写这个 application的文件就失效了
try {
PropertySource load = yamlPropertySourceLoader.load(“service”, resource, null);
PropertySource loadApp = yamlPropertySourceLoader.load(“app”, appResource, null);
sources.addLast(load);
sources.addLast(loadApp);
configurer.setPropertySources(sources);
return configurer;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
优点:网上大多数是这样写的
缺点:如果要多个yaml文件的话,需要把需要的都写在这里,这个方法会让spring默认的文件失效