java 配置中心_springboot项目接入配置中心,实现@ConfigurationProperties的bean属性刷新方案...

importcom.cmos.common.exception.GeneralException;importorg.springframework.boot.bind.PropertiesConfigurationFactory;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.core.convert.ConversionService;importorg.springframework.core.convert.support.DefaultConversionService;import org.springframework.core.env.*;importorg.springframework.validation.Validator;importorg.springframework.validation.beanvalidation.SpringValidatorAdapter;importjavax.validation.Validation;import staticorg.springframework.core.annotation.AnnotatedElementUtils.getMergedAnnotation;/***@authorhujunzheng

* @create 2018-07-03 18:01

*

* 不强依赖ConfigurationProperties,进行配置注入

**/

public class RelaxedConfigurationBinder{private final PropertiesConfigurationFactoryfactory;publicRelaxedConfigurationBinder(T object) {this(new PropertiesConfigurationFactory<>(object));

}public RelaxedConfigurationBinder(Classtype) {this(new PropertiesConfigurationFactory<>(type));

}public static RelaxedConfigurationBinderwith(T object) {return new RelaxedConfigurationBinder<>(object);

}public static RelaxedConfigurationBinder with(Classtype) {return new RelaxedConfigurationBinder<>(type);

}public RelaxedConfigurationBinder(PropertiesConfigurationFactoryfactory) {this.factory =factory;

ConfigurationProperties properties= getMergedAnnotation(factory.getObjectType(), ConfigurationProperties.class);

javax.validation.Validator validator=Validation.buildDefaultValidatorFactory().getValidator();

factory.setValidator(newSpringValidatorAdapter(validator));

factory.setConversionService(newDefaultConversionService());if (null !=properties) {

factory.setIgnoreNestedProperties(properties.ignoreNestedProperties());

factory.setIgnoreInvalidFields(properties.ignoreInvalidFields());

factory.setIgnoreUnknownFields(properties.ignoreUnknownFields());

factory.setTargetName(properties.prefix());

factory.setExceptionIfInvalid(properties.exceptionIfInvalid());

}

}public RelaxedConfigurationBindersetTargetName(String targetName) {

factory.setTargetName(targetName);return this;

}public RelaxedConfigurationBinder setPropertySources(PropertySource>... propertySources) {

MutablePropertySources sources= newMutablePropertySources();for (PropertySource>propertySource : propertySources) {

sources.addLast(propertySource);

}

factory.setPropertySources(sources);return this;

}public RelaxedConfigurationBindersetPropertySources(Environment environment) {

factory.setPropertySources(((ConfigurableEnvironment) environment).getPropertySources());return this;

}public RelaxedConfigurationBindersetPropertySources(PropertySources propertySources) {

factory.setPropertySources(propertySources);return this;

}public RelaxedConfigurationBindersetConversionService(ConversionService conversionService) {

factory.setConversionService(conversionService);return this;

}public RelaxedConfigurationBindersetValidator(Validator validator) {

factory.setValidator(validator);return this;

}public RelaxedConfigurationBinder setResolvePlaceholders(booleanresolvePlaceholders) {

factory.setResolvePlaceholders(resolvePlaceholders);return this;

}public T doBind() throwsGeneralException {try{returnfactory.getObject();

}catch(Exception ex) {throw new GeneralException("配置绑定失败!", ex);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值