本文整理了Java中org.springframework.beans.factory.config.YamlPropertiesFactoryBean
类的一些代码示例,展示了YamlPropertiesFactoryBean
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。YamlPropertiesFactoryBean
类的具体详情如下:
包路径:org.springframework.beans.factory.config.YamlPropertiesFactoryBean
类名称:YamlPropertiesFactoryBean
[英]Factory for java.util.Properties that reads from a YAML source, exposing a flat structure of String property values.
YAML is a nice human-readable format for configuration, and it has some useful hierarchical properties. It's more or less a superset of JSON, so it has a lot of similar features.
Note: All exposed values are of type String for access through the common Properties#getProperty method (e.g. in configuration property resolution through PropertyResourceConfigurer#setProperties(Properties)). If this is not desirable, use YamlMapFactoryBean instead.
The Properties created by this factory have nested paths for hierarchical objects, so for instance this YAML
environments:
dev:
url: http://dev.bar.com
name: Developer Setup
prod:
url: http://foo.bar.com
name: My Cool App
is transformed into these properties:
environments.dev.url=http://dev.bar.com
environments.dev.name=Developer Setup
environments.prod.url=http://foo.bar.com
environments.prod.name=My Cool App
Lists are split as property keys with []
dereferencers, for example this YAML:
servers:
- dev.bar.com
- foo.bar.com
becomes properties like this:
servers[0]=dev.bar.com
servers[1]=foo.bar.com
Requires SnakeYAML 1.18 or higher, as of Spring Framework 5.0.6.
[中]java工厂。util。从YAML源读取的属性,公开字符串属性值的平面结构。
YAML是一种很好的人类可读的配置格式,它具有一些有用的层次属性。它或多或少是JSON的超集,所以它有很多类似的特性。
注意:所有公开的值都是字符串类型,可以通过公共属性#getProperty方法进行访问(例如,通过PropertyResourceConfigurator#setProperties(Properties)进行配置属性解析)。如果这不可取,请改用YamlMapFactoryBean。
此工厂创建的属性具有层次对象的嵌套路径,因此,例如,此YAML
environments:
dev:
url: http://dev.bar.com
name: Developer Setup
prod:
url: http://foo.bar.com
name: My Cool App
被转换为以下属性:
environments.dev.url=http://dev.bar.com
environments.dev.name=Developer Setup
environments.prod.url=http://foo.bar.com
environments.prod.name=My Cool App
列表被拆分为带有[]
解引用器的属性键,例如,这个YAML:
servers:
- dev.bar.com
- foo.bar.com
变成如下属性:
servers[0]=dev.bar.com
servers[1]=foo.bar.com
从Spring Framework 5.0.6开始,需要SnakeYAML 1.18或更高版本。
代码示例来源:origin: stackoverflow.com
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("default.yml"));
propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
return propertySourcesPlaceholderConfigurer;
}
代码示例来源:origin: spring-io/initializr
private static Properties loadProperties(Resource resource) {
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
yamlFactory.setResources(resource);
yamlFactory.afterPropertiesSet();
return yamlFactory.getObject();
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public void afterPropertiesSet() {
if (isSingleton()) {
this.properties = createProperties();
}
}
代码示例来源:origin: spring-cloud/spring-cloud-kubernetes
static Function<String, Properties> yamlParserGenerator(
final String[] profiles) {
return s -> {
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
yamlFactory.setDocumentMatchers(properties -> {
String profileProperty = properties.getProperty("spring.profiles");
if (profileProperty != null && profileProperty.length() > 0) {
return asList(profiles).contains(profileProperty) ? FOUND : NOT_FOUND;
}
else {
return ABSTAIN;
}
});
yamlFactory.setResources(new ByteArrayResource(s.getBytes()));
return yamlFactory.getObject();
};
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testLoadNonExistentResource() {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResolutionMethod(ResolutionMethod.OVERRIDE_AND_IGNORE);
factory.setResources(new ClassPathResource("no-such-file.yml"));
Properties properties = factory.getObject();
assertThat(properties.size(), equalTo(0));
}
代码示例来源:origin: org.apereo.cas/cas-server-core-configuration-api
/**
* Load yaml properties map.
*
* @param resource the resource
* @return the map
*/
public static Map loadYamlProperties(final Resource... resource) {
val factory = new YamlPropertiesFactoryBean();
factory.setResolutionMethod(YamlProcessor.ResolutionMethod.OVERRIDE);
factory.setResources(resource);
factory.setSingleton(true);
factory.afterPropertiesSet();
return factory.getObject();
}
}
代码示例来源:origin: stackoverflow.com
factoryBean = new YamlPropertiesFactoryBean();
factoryBean.setSingleton(true); // optional depends on your use-case
factoryBean.setResources(resource);
properties = factoryBean.getObject();
代码示例来源:origin: alien4cloud/alien4cloud
/**
* Get a singleton instance of {@link YamlPropertiesFactoryBean}.
*
* @param resourceLoader The loader to use to find the yaml file.
* @return an instance of the {@link YamlPropertiesFactoryBean}.
*/
public static YamlPropertiesFactoryBean get(ResourceLoader resourceLoader) {
if (INSTANCE == null) {
for (String searchLocation : SEARCH_LOCATIONS) {
Resource resource = resourceLoader.getResource(searchLocation + ALIEN_CONFIGURATION_YAML);
if (resource != null && resource.exists()) {
log.info("Loading Alien 4 Cloud configuration from {}", resource.getDescription());
INSTANCE = new YamlPropertiesFactoryBean();
INSTANCE.setResources(new Resource[] { resource });
return INSTANCE;
}
}
}
return INSTANCE;
}
}
代码示例来源:origin: io.cloudsoft.brooklyn.tosca/brooklyn-tosca-transformer
@Bean(name = {"alienconfig", "elasticsearchConfig"})
public static Properties alienConfig(BeanFactory beans, ResourceLoader resourceLoader) throws IOException {
ManagementContext mgmt = null;
if (beans.containsBean("brooklynManagementContext")) {
mgmt = beans.getBean("brooklynManagementContext", ManagementContext.class);
}
return AlienBrooklynYamlPropertiesFactoryBeanFactory.get(mgmt, resourceLoader).getObject();
}
代码示例来源:origin: com.bosch.bis.base/bis-test
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setProperties(new YamlPropertiesFactoryBean().getObject());
return propertySourcesPlaceholderConfigurer;
}
}
代码示例来源:origin: spring-projects/spring-framework
@Override
@Nullable
public Properties getObject() {
return (this.properties != null ? this.properties : createProperties());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testLoadResourceWithSelectedDocuments() {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(new ByteArrayResource(
"foo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".getBytes()));
factory.setDocumentMatchers(properties -> ("bag".equals(properties.getProperty("foo")) ?
MatchStatus.FOUND : MatchStatus.NOT_FOUND));
Properties properties = factory.getObject();
assertThat(properties.getProperty("foo"), equalTo("bag"));
assertThat(properties.getProperty("spam"), equalTo("bad"));
}
代码示例来源:origin: io.cloudsoft.brooklyn.tosca/brooklyn-tosca-transformer
/**
* Get a singleton instance of {@link YamlPropertiesFactoryBean}.
* @param mgmt
*
* @param resourceLoader The loader to use to find the yaml file.
* @return an instance of the {@link YamlPropertiesFactoryBean} or null if one could not be loaded.
*/
public static YamlPropertiesFactoryBean get(ManagementContext mgmt, ResourceLoader resourceLoader) {
if (INSTANCE == null) {
String configFile = System.getProperty(ALIEN_CONFIG_FILE.getName());
if (configFile==null && mgmt!=null) {
configFile = mgmt.getConfig().getConfig(ALIEN_CONFIG_FILE);
}
if (Strings.isNonBlank(configFile)) {
log.info("Loading A4C config from "+configFile);
final byte[] resourceArr = new ResourceUtils(AlienBrooklynYamlPropertiesFactoryBeanFactory.class)
.getResourceAsString(configFile)
.getBytes(Charsets.UTF_8);
Resource resource = new ByteArrayResource(resourceArr, configFile);
INSTANCE = new YamlPropertiesFactoryBean();
INSTANCE.setResources(resource);
} else {
INSTANCE = AlienYamlPropertiesFactoryBeanFactory.get(resourceLoader);
}
}
return INSTANCE;
}
}
代码示例来源:origin: alien4cloud/alien4cloud
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
YamlPropertiesFactoryBean propertiesFactoryBean = AlienYamlPropertiesFactoryBeanFactory.get(context.getResourceLoader());
Object ldapEnabled = propertiesFactoryBean.getObject().get("ldap.enabled");
if (ldapEnabled != null && ldapEnabled instanceof Boolean) {
return ((Boolean) ldapEnabled).booleanValue();
}
return false;
}
}
代码示例来源:origin: org.springframework/spring-beans
@Override
@Nullable
public Properties getObject() {
return (this.properties != null ? this.properties : createProperties());
}
代码示例来源:origin: spring-projects/spring-framework
@Test(expected = DuplicateKeyException.class)
public void testLoadResourcesWithInternalOverride() {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(new ByteArrayResource(
"foo: bar\nspam:\n foo: baz\nfoo: bucket".getBytes()));
factory.getObject();
}
代码示例来源:origin: spring-cloud/spring-cloud-dataflow
private static void contributeDefaults(Map<String, Object> defaults, Resource resource) {
if (resource.exists()) {
YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
yamlPropertiesFactoryBean.setResources(resource);
yamlPropertiesFactoryBean.afterPropertiesSet();
Properties p = yamlPropertiesFactoryBean.getObject();
for (Object k : p.keySet()) {
String key = k.toString();
defaults.put(key, p.get(key));
}
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testLoadResourceWithDefaultMatchSkippingMissedMatch() {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setMatchDefault(true);
factory.setResources(new ByteArrayResource(
"one: two\n---\nfoo: bag\nspam: bad\n---\nfoo: bar\nspam: baz".getBytes()));
factory.setDocumentMatchers(properties -> {
if (!properties.containsKey("foo")) {
return MatchStatus.ABSTAIN;
}
return ("bag".equals(properties.getProperty("foo")) ?
MatchStatus.FOUND : MatchStatus.NOT_FOUND);
});
Properties properties = factory.getObject();
assertThat(properties.getProperty("foo"), equalTo("bag"));
assertThat(properties.getProperty("spam"), equalTo("bad"));
assertThat(properties.getProperty("one"), equalTo("two"));
}
代码示例来源:origin: alien4cloud/alien4cloud
@Bean
public static PropertySourcesPlaceholderConfigurer properties(ResourceLoader resourceLoader) {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = AlienYamlPropertiesFactoryBeanFactory.get(resourceLoader);
propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
return propertySourcesPlaceholderConfigurer;
}
代码示例来源:origin: org.springframework/spring-beans
@Override
public void afterPropertiesSet() {
if (isSingleton()) {
this.properties = createProperties();
}
}
内容来源于网络,如有侵权,请联系作者删除!