下面列出了org.springframework.boot.bind.PropertiesConfigurationFactory#bindPropertiesToTarget ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
if (SOFABootEnvUtils.isSpringCloudBootstrapEnvironment(environment)) {
return;
}
// set loggingPath
String loggingPath = environment.getProperty("logging.path");
if (StringUtils.isNotBlank(loggingPath)) {
System.setProperty("logging.path", loggingPath);
}
// check spring.application.name
String applicationName = environment
.getProperty(SofaTracerConfiguration.TRACER_APPNAME_KEY);
Assert.isTrue(!StringUtils.isBlank(applicationName),
SofaTracerConfiguration.TRACER_APPNAME_KEY + " must be configured!");
SofaTracerConfiguration.setProperty(SofaTracerConfiguration.TRACER_APPNAME_KEY,
applicationName);
SofaTracerProperties tempTarget = new SofaTracerProperties();
PropertiesConfigurationFactory<SofaTracerProperties> binder = new PropertiesConfigurationFactory<SofaTracerProperties>(
tempTarget);
ConfigurationProperties configurationPropertiesAnnotation = this
.getConfigurationPropertiesAnnotation(tempTarget);
if (configurationPropertiesAnnotation != null
&& StringUtils.isNotBlank(configurationPropertiesAnnotation.prefix())) {
//consider compatible Spring Boot 1.5.X and 2.x
binder.setIgnoreInvalidFields(configurationPropertiesAnnotation.ignoreInvalidFields());
binder.setIgnoreUnknownFields(configurationPropertiesAnnotation.ignoreUnknownFields());
binder.setTargetName(configurationPropertiesAnnotation.prefix());
} else {
binder.setTargetName(SofaTracerProperties.SOFA_TRACER_CONFIGURATION_PREFIX);
}
binder.setConversionService(new DefaultConversionService());
binder.setPropertySources(environment.getPropertySources());
try {
binder.bindPropertiesToTarget();
} catch (BindException ex) {
throw new IllegalStateException("Cannot bind to SofaTracerProperties", ex);
}
//properties convert to tracer
SofaTracerConfiguration.setProperty(
SofaTracerConfiguration.DISABLE_MIDDLEWARE_DIGEST_LOG_KEY,
tempTarget.getDisableDigestLog());
SofaTracerConfiguration.setProperty(SofaTracerConfiguration.DISABLE_DIGEST_LOG_KEY,
tempTarget.getDisableConfiguration());
SofaTracerConfiguration.setProperty(SofaTracerConfiguration.TRACER_GLOBAL_ROLLING_KEY,
tempTarget.getTracerGlobalRollingPolicy());
SofaTracerConfiguration.setProperty(SofaTracerConfiguration.TRACER_GLOBAL_LOG_RESERVE_DAY,
tempTarget.getTracerGlobalLogReserveDay());
//stat log interval
SofaTracerConfiguration.setProperty(SofaTracerConfiguration.STAT_LOG_INTERVAL,
tempTarget.getStatLogInterval());
//baggage length
SofaTracerConfiguration.setProperty(
SofaTracerConfiguration.TRACER_PENETRATE_ATTRIBUTE_MAX_LENGTH,
tempTarget.getBaggageMaxLength());
SofaTracerConfiguration.setProperty(
SofaTracerConfiguration.TRACER_SYSTEM_PENETRATE_ATTRIBUTE_MAX_LENGTH,
tempTarget.getBaggageMaxLength());
//sampler config
if (tempTarget.getSamplerName() != null) {
SofaTracerConfiguration.setProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_NAME_KEY,
tempTarget.getSamplerName());
}
if (StringUtils.isNotBlank(tempTarget.getSamplerCustomRuleClassName())) {
SofaTracerConfiguration.setProperty(
SofaTracerConfiguration.SAMPLER_STRATEGY_CUSTOM_RULE_CLASS_NAME,
tempTarget.getSamplerCustomRuleClassName());
}
SofaTracerConfiguration.setProperty(
SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY,
String.valueOf(tempTarget.getSamplerPercentage()));
SofaTracerConfiguration.setProperty(SofaTracerConfiguration.JSON_FORMAT_OUTPUT,
String.valueOf(tempTarget.isJsonOutput()));
}
private EagleConfig getEagleConfig(DefaultListableBeanFactory beanFactory) {
EagleConfig bean = new EagleConfig();
Object target = bean;
PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<Object>(target);
factory.setPropertySources(deducePropertySources(beanFactory));
//factory.setValidator(determineValidator(bean));
factory.setConversionService(conversionService);
factory.setIgnoreInvalidFields(false);
factory.setIgnoreUnknownFields(true);
factory.setIgnoreNestedProperties(false);
factory.setTargetName("eagle");
try {
factory.bindPropertiesToTarget();
} catch (Exception ex) {
throw new EagleFrameException(ex);
}
return bean;
}
@Override
public void parse(File file, ContainerCreationContext context) {
try {
ContainerSource arg = new ContainerSource();
PropertySourcesLoader loader = new PropertySourcesLoader();
loader.load(new FileSystemResource(file));
MutablePropertySources loaded = loader.getPropertySources();
PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<>(arg);
factory.setPropertySources(loaded);
factory.setConversionService(defaultConversionService);
factory.bindPropertiesToTarget();
arg.getInclude().forEach(a -> parse(new File(file.getParent(), a), context));
context.addCreateContainerArg(arg);
} catch (Exception e) {
log.error("can't parse configuration", e.getMessage());
}
}
@Override
public void parse(Map<String, Object> map, ContainerSource arg) {
try {
PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<>(arg);
MutablePropertySources propertySources = new MutablePropertySources();
PropertySource propertySource = new MapPropertySource("inner", map);
propertySources.addFirst(propertySource);
factory.setPropertySources(propertySources);
factory.setConversionService(defaultConversionService);
factory.bindPropertiesToTarget();
log.debug("result of parsing msp {}", arg);
} catch (Exception e) {
log.error("", e);
}
}
private <T> T getDruidConfig(String prefix, Class<T> claz) {
PropertiesConfigurationFactory<T> factory = new PropertiesConfigurationFactory<T>(claz);
factory.setPropertySources(environment.getPropertySources());
factory.setConversionService(environment.getConversionService());
factory.setIgnoreInvalidFields(false);
factory.setIgnoreUnknownFields(true);
factory.setIgnoreNestedProperties(false);
factory.setTargetName(prefix);
try {
factory.bindPropertiesToTarget();
return factory.getObject();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private <T> T getDruidConfig(String prefix, Class<T> claz) {
PropertiesConfigurationFactory<T> factory = new PropertiesConfigurationFactory<T>(claz);
factory.setPropertySources(environment.getPropertySources());
factory.setConversionService(environment.getConversionService());
factory.setIgnoreInvalidFields(false);
factory.setIgnoreUnknownFields(true);
factory.setIgnoreNestedProperties(false);
factory.setTargetName(prefix);
try {
factory.bindPropertiesToTarget();
return factory.getObject();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private <T> T getPropertiesConfigurationBean(String targetName, Class<T> types) {
PropertiesConfigurationFactory<T> factory = new PropertiesConfigurationFactory<T>(types);
factory.setPropertySources(environment.getPropertySources());
factory.setConversionService(environment.getConversionService());
factory.setIgnoreInvalidFields(true);
factory.setIgnoreUnknownFields(true);
factory.setIgnoreNestedProperties(false);
factory.setTargetName(targetName);
try {
factory.bindPropertiesToTarget();
return factory.getObject();
} catch (Exception e) {
throw new RuntimeException(e);
}
}