gpt4 book ai didi

org.springframework.beans.factory.config.YamlPropertiesFactoryBean.()方法的使用及代码示例

转载 作者:知者 更新时间:2024-03-17 05:04:40 27 4
gpt4 key购买 nike

本文整理了Java中org.springframework.beans.factory.config.YamlPropertiesFactoryBean.<init>()方法的一些代码示例,展示了YamlPropertiesFactoryBean.<init>()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。YamlPropertiesFactoryBean.<init>()方法的具体详情如下:
包路径:org.springframework.beans.factory.config.YamlPropertiesFactoryBean
类名称:YamlPropertiesFactoryBean
方法名:<init>

YamlPropertiesFactoryBean.<init>介绍

暂无

代码示例

代码示例来源:origin: spring-cloud-incubator/spring-cloud-alibaba

private Properties loadDiamondData(String dataId, String diamondGroup) {
  try {
    String data = ConfigService.getConfig(dataId, diamondGroup, 3000L);
    if (StringUtils.isEmpty(data)) {
      return null;
    }
    if (dataId.endsWith(".properties")) {
      Properties properties = new Properties();
      logger.info(String.format("Loading acm data, dataId: '%s', group: '%s'",
        dataId, diamondGroup));
      properties.load(new StringReader(data));
      return properties;
    } else if (dataId.endsWith(".yaml") || dataId.endsWith(".yml")) {
      YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
      yamlFactory.setResources(new ByteArrayResource(data.getBytes()));
      return yamlFactory.getObject();
    }
  } catch (Exception e) {
    if (e instanceof ConfigException) {
      logger.error("DIAMOND-100500:" + dataId + ", " + e.toString(), e);
    } else {
      logger.error("DIAMOND-100500:" + dataId, e);
    }
  }
  return null;
}

代码示例来源: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 testLoadArrayOfString() {
  YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
  factory.setResources(new ByteArrayResource("foo:\n- bar\n- baz".getBytes()));
  Properties properties = factory.getObject();
  assertThat(properties.getProperty("foo[0]"), equalTo("bar"));
  assertThat(properties.getProperty("foo[1]"), equalTo("baz"));
  assertThat(properties.get("foo"), is(nullValue()));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testLoadArrayOfInteger() {
  YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
  factory.setResources(new ByteArrayResource("foo:\n- 1\n- 2".getBytes()));
  Properties properties = factory.getObject();
  assertThat(properties.getProperty("foo[0]"), equalTo("1"));
  assertThat(properties.getProperty("foo[1]"), equalTo("2"));
  assertThat(properties.get("foo"), is(nullValue()));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testLoadResourceWithMultipleDocuments() {
  YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
  factory.setResources(new ByteArrayResource(
      "foo: bar\nspam: baz\n---\nfoo: bag".getBytes()));
  Properties properties = factory.getObject();
  assertThat(properties.getProperty("foo"), equalTo("bag"));
  assertThat(properties.getProperty("spam"), equalTo("baz"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testLoadArrayOfObject() {
  YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
  factory.setResources(new ByteArrayResource(
      "foo:\n- bar:\n    spam: crap\n- baz\n- one: two\n  three: four".getBytes()
  ));
  Properties properties = factory.getObject();
  assertThat(properties.getProperty("foo[0].bar.spam"), equalTo("crap"));
  assertThat(properties.getProperty("foo[1]"), equalTo("baz"));
  assertThat(properties.getProperty("foo[2].one"), equalTo("two"));
  assertThat(properties.getProperty("foo[2].three"), equalTo("four"));
  assertThat(properties.get("foo"), is(nullValue()));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testLoadNull() {
  YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
  factory.setResources(new ByteArrayResource("foo: bar\nspam:".getBytes()));
  Properties properties = factory.getObject();
  assertThat(properties.getProperty("foo"), equalTo("bar"));
  assertThat(properties.getProperty("spam"), equalTo(""));
}

代码示例来源: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: spring-projects/spring-framework

@Test
public void testLoadResourcesWithOverride() {
  YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
  factory.setResources(
      new ByteArrayResource("foo: bar\nspam:\n  foo: baz".getBytes()),
      new ByteArrayResource("foo:\n  bar: spam".getBytes()));
  Properties properties = factory.getObject();
  assertThat(properties.getProperty("foo"), equalTo("bar"));
  assertThat(properties.getProperty("spam.foo"), equalTo("baz"));
  assertThat(properties.getProperty("foo.bar"), equalTo("spam"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testLoadResource() {
  YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
  factory.setResources(new ByteArrayResource("foo: bar\nspam:\n  foo: baz".getBytes()));
  Properties properties = factory.getObject();
  assertThat(properties.getProperty("foo"), equalTo("bar"));
  assertThat(properties.getProperty("spam.foo"), equalTo("baz"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testLoadEmptyArrayValue() {
  YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
  factory.setResources(new ByteArrayResource("a: alpha\ntest: []".getBytes()));
  Properties properties = factory.getObject();
  assertThat(properties.getProperty("a"), equalTo("alpha"));
  assertThat(properties.getProperty("test"), equalTo(""));
}

代码示例来源: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-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: 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-projects/spring-framework

@Test(expected = DuplicateKeyException.class)
public void testLoadResourcesWithNestedInternalOverride() {
  YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
  factory.setResources(new ByteArrayResource(
      "foo:\n  bar: spam\n  foo: baz\nbreak: it\nfoo: bucket".getBytes()));
  factory.getObject();
}

代码示例来源: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: spring-projects/spring-framework

@Test
public void testLoadResourceWithDefaultMatch() {
  YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
  factory.setMatchDefault(true);
  factory.setResources(new ByteArrayResource(
      "one: two\n---\nfoo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".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: spring-projects/spring-framework

@Test
public void testLoadResourceWithoutDefaultMatch() {
  YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
  factory.setMatchDefault(false);
  factory.setResources(new ByteArrayResource(
      "one: two\n---\nfoo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".getBytes()));
  factory.setDocumentMatchers(new DocumentMatcher() {
    @Override
    public MatchStatus matches(Properties 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"), nullValue());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testBadResource() {
  YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
  factory.setResources(new ByteArrayResource(
      "foo: bar\ncd\nspam:\n  foo: baz".getBytes()));
  this.exception.expect(ScannerException.class);
  this.exception.expectMessage("line 3, column 1");
  factory.getObject();
}

代码示例来源:origin: spring-io/initializr

private static Properties loadProperties(Resource resource) {
  YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
  yamlFactory.setResources(resource);
  yamlFactory.afterPropertiesSet();
  return yamlFactory.getObject();
}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com