Java Code Examples for org.springframework.boot.context.properties.source.ConfigurationPropertySources#from()

The following examples show how to use org.springframework.boot.context.properties.source.ConfigurationPropertySources#from() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExternalizedConfigurationBinderBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 7 votes vote down vote up
public static void main(String[] args) throws IOException {
    // application.properties 文件资源 classpath 路径
    String location = "application.properties";
    // 编码化的 Resource 对象(解决乱码问题)
    EncodedResource resource = new EncodedResource(new ClassPathResource(location), "UTF-8");
    // 加载 application.properties 文件,转化为 Properties 对象
    Properties properties = PropertiesLoaderUtils.loadProperties(resource);
    // 创建 Properties 类型的 PropertySource
    PropertiesPropertySource propertySource = new PropertiesPropertySource("map", properties);
    // 转化为 Spring Boot 2 外部化配置源 ConfigurationPropertySource 集合
    Iterable<ConfigurationPropertySource> propertySources = ConfigurationPropertySources.from(propertySource);
    // 创建 Spring Boot 2 Binder 对象(设置 ConversionService ,扩展类型转换能力)
    Binder binder = new Binder(propertySources, null, conversionService());
    // 执行绑定,返回绑定结果
    BindResult<User> bindResult = binder.bind("user", User.class);
    // 获取绑定对象
    User user = bindResult.get();
    // 输出结果
    System.out.println(user);

}
 
Example 2
Source File: ApplicationPropertiesTests.java    From github-release-notes-generator with Apache License 2.0 6 votes vote down vote up
@Test
public void loadYaml() throws Exception {
	YamlPropertySourceLoader yamlLoader = new YamlPropertySourceLoader();
	List<PropertySource<?>> yaml = yamlLoader.load("application",
			new ClassPathResource("test-application.yml", getClass()));
	Binder binder = new Binder(ConfigurationPropertySources.from(yaml));
	ApplicationProperties properties = binder.bind("releasenotes", ApplicationProperties.class).get();
	Github github = properties.getGithub();
	assertThat(github.getUsername()).isEqualTo("testuser");
	assertThat(github.getPassword()).isEqualTo("testpass");
	assertThat(github.getOrganization()).isEqualTo("testorg");
	assertThat(github.getRepository()).isEqualTo("testrepo");
	List<Section> sections = properties.getSections();
	assertThat(sections.get(0).getTitle()).isEqualTo("New Features");
	assertThat(sections.get(0).getEmoji()).isEqualTo(":star:");
	assertThat(sections.get(0).getLabels()).containsExactly("enhancement");
}
 
Example 3
Source File: JasyptEncryptorConfigurationProperties.java    From jasypt-spring-boot with MIT License 6 votes vote down vote up
public static JasyptEncryptorConfigurationProperties bindConfigProps(ConfigurableEnvironment environment) {
    final BindHandler handler = new IgnoreErrorsBindHandler(BindHandler.DEFAULT);
    final MutablePropertySources propertySources = environment.getPropertySources();
    final Binder binder = new Binder(ConfigurationPropertySources.from(propertySources),
            new PropertySourcesPlaceholdersResolver(propertySources),
            ApplicationConversionService.getSharedInstance());
    final JasyptEncryptorConfigurationProperties config = new JasyptEncryptorConfigurationProperties();

    final ResolvableType type = ResolvableType.forClass(JasyptEncryptorConfigurationProperties.class);
    final Annotation annotation = AnnotationUtils.findAnnotation(JasyptEncryptorConfigurationProperties.class,
            ConfigurationProperties.class);
    final Annotation[] annotations = new Annotation[]{annotation};
    final Bindable<?> target = Bindable.of(type).withExistingValue(config).withAnnotations(annotations);

    binder.bind("jasypt.encryptor", target, handler);
    return config;
}
 
Example 4
Source File: FunctionalConfigurationPropertiesBinder.java    From spring-fu with Apache License 2.0 5 votes vote down vote up
public FunctionalConfigurationPropertiesBinder(ConfigurableApplicationContext context) {
	PropertySources propertySources = new FunctionalPropertySourcesDeducer(context).getPropertySources();
	this.context = context;
	this.binder = new Binder(ConfigurationPropertySources.from(propertySources),
       				new PropertySourcesPlaceholdersResolver(propertySources),
       				null,
			(registry) -> context.getBeanFactory().copyRegisteredEditorsTo(registry));
}
 
Example 5
Source File: CustomizedConfigurationPropertiesBinder.java    From apollo-use-cases with Apache License 2.0 4 votes vote down vote up
private Iterable<ConfigurationPropertySource> getConfigurationPropertySources() {
  return ConfigurationPropertySources.from(this.propertySources);
}
 
Example 6
Source File: MangoConfigFactory.java    From mango-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
private static Iterable<ConfigurationPropertySource> getConfigurationPropertySources(PropertySources propertySources) {
    return ConfigurationPropertySources.from(propertySources);
}