下面列出了org.springframework.boot.context.properties.source.ConfigurationPropertySources#from ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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");
}
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);
}
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;
}
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));
}
private Iterable<ConfigurationPropertySource> getConfigurationPropertySources() {
return ConfigurationPropertySources.from(this.propertySources);
}
private static Iterable<ConfigurationPropertySource> getConfigurationPropertySources(PropertySources propertySources) {
return ConfigurationPropertySources.from(propertySources);
}