Java Code Examples for org.springframework.boot.bind.PropertiesConfigurationFactory#bindPropertiesToTarget()

The following examples show how to use org.springframework.boot.bind.PropertiesConfigurationFactory#bindPropertiesToTarget() . 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: SofaTracerConfigurationListener.java    From sofa-tracer with Apache License 2.0 6 votes vote down vote up
@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()));
}
 
Example 2
Source File: EagleBeanFactoryPostProcessor.java    From eagle with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: DefaultParser.java    From haven-platform with Apache License 2.0 6 votes vote down vote up
@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());
    }
}
 
Example 4
Source File: DefaultParser.java    From haven-platform with Apache License 2.0 6 votes vote down vote up
@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);
    }

}
 
Example 5
Source File: DataSourceAutoConfiguration.java    From spring-boot-starter-dao with Apache License 2.0 5 votes vote down vote up
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);
	}
}
 
Example 6
Source File: GeneratorMain.java    From spring-boot-starter-dao with Apache License 2.0 5 votes vote down vote up
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);
	}
}
 
Example 7
Source File: DubboAutoConfiguration.java    From spring-boot-starter-dubbo with Apache License 2.0 5 votes vote down vote up
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);
	}
}