下面列出了怎么用org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void bind(Map<String, Object> configurationProperties, boolean ignoreUnknownFields,
boolean ignoreInvalidFields, Object configurationBean) {
Iterable<PropertySource<?>> propertySources = asList(new MapPropertySource("internal", configurationProperties));
// Converts ConfigurationPropertySources
Iterable<ConfigurationPropertySource> configurationPropertySources = from(propertySources);
// Wrap Bindable from DubboConfig instance
Bindable bindable = Bindable.ofInstance(configurationBean);
Binder binder = new Binder(configurationPropertySources, new PropertySourcesPlaceholdersResolver(propertySources));
// Get BindHandler
BindHandler bindHandler = getBindHandler(ignoreUnknownFields, ignoreInvalidFields);
// Bind
binder.bind("", bindable, bindHandler);
}
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;
}
@SuppressWarnings("unchecked")
private void bindToDefault(String binding) {
T extendedBindingPropertiesTarget = (T) BeanUtils
.instantiateClass(this.getExtendedPropertiesEntryClass());
Binder binder = new Binder(
ConfigurationPropertySources
.get(this.applicationContext.getEnvironment()),
new PropertySourcesPlaceholdersResolver(
this.applicationContext.getEnvironment()),
IntegrationUtils.getConversionService(
this.applicationContext.getBeanFactory()),
null);
binder.bind(this.getDefaultsPrefix(),
Bindable.ofInstance(extendedBindingPropertiesTarget));
this.bindings.put(binding, extendedBindingPropertiesTarget);
}
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));
}
@Bean
@ConfigurationProperties(prefix = "spring.cloud.stream.kafka.streams.binder")
public KafkaStreamsBinderConfigurationProperties binderConfigurationProperties(
KafkaProperties kafkaProperties, ConfigurableEnvironment environment,
BindingServiceProperties properties, ConfigurableApplicationContext context) throws Exception {
final Map<String, BinderConfiguration> binderConfigurations = getBinderConfigurations(
properties);
for (Map.Entry<String, BinderConfiguration> entry : binderConfigurations
.entrySet()) {
final BinderConfiguration binderConfiguration = entry.getValue();
final String binderType = binderConfiguration.getBinderType();
if (binderType != null && (binderType.equals(KSTREAM_BINDER_TYPE)
|| binderType.equals(KTABLE_BINDER_TYPE)
|| binderType.equals(GLOBALKTABLE_BINDER_TYPE))) {
Map<String, Object> binderProperties = new HashMap<>();
this.flatten(null, binderConfiguration.getProperties(), binderProperties);
environment.getPropertySources().addFirst(
new MapPropertySource(entry.getKey() + "-kafkaStreamsBinderEnv", binderProperties));
Binder binder = new Binder(ConfigurationPropertySources.get(environment),
new PropertySourcesPlaceholdersResolver(environment),
IntegrationUtils.getConversionService(context.getBeanFactory()), null);
final Constructor<KafkaStreamsBinderConfigurationProperties> kafkaStreamsBinderConfigurationPropertiesConstructor =
ReflectionUtils.accessibleConstructor(KafkaStreamsBinderConfigurationProperties.class, KafkaProperties.class);
final KafkaStreamsBinderConfigurationProperties kafkaStreamsBinderConfigurationProperties =
BeanUtils.instantiateClass(kafkaStreamsBinderConfigurationPropertiesConstructor, kafkaProperties);
final BindResult<KafkaStreamsBinderConfigurationProperties> bind = binder.bind("spring.cloud.stream.kafka.streams.binder", Bindable.ofInstance(kafkaStreamsBinderConfigurationProperties));
context.getBeanFactory().registerSingleton(
entry.getKey() + "-KafkaStreamsBinderConfigurationProperties",
bind.get());
}
}
return new KafkaStreamsBinderConfigurationProperties(kafkaProperties);
}
@Override
public void setEnvironment(final Environment environment) {
final Iterable<ConfigurationPropertySource> sources =
from(((ConfigurableEnvironment) environment).getPropertySources());
final Binder binder = new Binder(sources,
new PropertySourcesPlaceholdersResolver(environment));
this.properties = Defaulting.withDefaults(binder.bindOrCreate("riptide", RiptideProperties.class));
}
private void bindToDefault(String binding) {
BindingProperties bindingPropertiesTarget = new BindingProperties();
Binder binder = new Binder(
ConfigurationPropertySources
.get(this.applicationContext.getEnvironment()),
new PropertySourcesPlaceholdersResolver(
this.applicationContext.getEnvironment()),
IntegrationUtils.getConversionService(
this.applicationContext.getBeanFactory()),
null);
binder.bind("spring.cloud.stream.default",
Bindable.ofInstance(bindingPropertiesTarget));
this.bindings.put(binding, bindingPropertiesTarget);
}
private PropertySourcesPlaceholdersResolver getPropertySourcesPlaceholdersResolver() {
return new PropertySourcesPlaceholdersResolver(this.propertySources);
}
private static PropertySourcesPlaceholdersResolver getPropertySourcesPlaceholdersResolver(PropertySources propertySources) {
return new PropertySourcesPlaceholdersResolver(propertySources);
}