转载

聊聊NacosConfigAutoConfiguration

nacos-spring-boot-project/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/autoconfigure/NacosConfigAutoConfiguration.java

@ConditionalOnProperty(name = NacosConfigConstants.ENABLED, matchIfMissing = true)
@ConditionalOnMissingBean(name = CONFIG_GLOBAL_NACOS_PROPERTIES_BEAN_NAME)
@EnableConfigurationProperties(value = NacosConfigProperties.class)
@ConditionalOnClass(name = "org.springframework.boot.context.properties.bind.Binder")
@Import(value = {NacosConfigBootBeanDefinitionRegistrar.class})
@EnableNacosConfig
public class NacosConfigAutoConfiguration {

}
复制代码
  • NacosConfigAutoConfiguration导入了NacosConfigBootBeanDefinitionRegistrar

NacosConfigBootBeanDefinitionRegistrar

nacos-spring-boot-project/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/autoconfigure/NacosConfigBootBeanDefinitionRegistrar.java

@Configuration
public class NacosConfigBootBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar, BeanFactoryAware {

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory;
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
                .rootBeanDefinition(NacosBootConfigurationPropertiesBinder.class);
        defaultListableBeanFactory.registerBeanDefinition(NacosBootConfigurationPropertiesBinder.BEAN_NAME, beanDefinitionBuilder.getBeanDefinition());
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

    }
}
复制代码
  • NacosConfigBootBeanDefinitionRegistrar实现了ImportBeanDefinitionRegistrar, BeanFactoryAware接口;其setBeanFactory方法注册了nacosConfigurationPropertiesBinder

NacosBootConfigurationPropertiesBinder

nacos-spring-boot-project/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/binder/NacosBootConfigurationPropertiesBinder.java

public class NacosBootConfigurationPropertiesBinder extends NacosConfigurationPropertiesBinder {

    private final Logger logger = LoggerFactory.getLogger(NacosBootConfigurationPropertiesBinder.class);

    private ConfigurableApplicationContext context;
    private ConfigurationBeanFactoryMetadata beanFactoryMetadata;

    public NacosBootConfigurationPropertiesBinder(ConfigurableApplicationContext applicationContext) {
        super(applicationContext);
        this.context = applicationContext;
        this.beanFactoryMetadata = applicationContext.getBean(
                ConfigurationBeanFactoryMetadata.BEAN_NAME,
                ConfigurationBeanFactoryMetadata.class);
    }

    @Override
    protected void doBind(Object bean, String beanName, String dataId, String groupId, String configType,
                          NacosConfigurationProperties properties, String content, ConfigService configService) {
        String name = "nacos-bootstrap-" + beanName;
        NacosPropertySource propertySource = new NacosPropertySource(name, dataId, groupId, content, configType);
        context.getEnvironment().getPropertySources().addLast(propertySource);
        Binder binder = Binder.get(context.getEnvironment());
        ResolvableType type = getBeanType(bean, beanName);
        Bindable<?> target = Bindable.of(type).withExistingValue(bean);
        binder.bind(properties.prefix(), target);
        publishBoundEvent(bean, beanName, dataId, groupId, properties, content, configService);
        publishMetadataEvent(bean, beanName, dataId, groupId, properties);
        context.getEnvironment().getPropertySources().remove(name);
    }

    private ResolvableType getBeanType(Object bean, String beanName) {
        Method factoryMethod = this.beanFactoryMetadata.findFactoryMethod(beanName);
        if (factoryMethod != null) {
            return ResolvableType.forMethodReturnType(factoryMethod);
        }
        return ResolvableType.forClass(bean.getClass());
    }

}
复制代码
  • NacosBootConfigurationPropertiesBinder继承了NacosConfigurationPropertiesBinder;其doBind方法会创建NacosPropertySource,然后添加到context.getEnvironment().getPropertySources(),之后通过Binder.get(context.getEnvironment())的bind方法进行绑定
原文  https://juejin.im/post/5d8cd9c46fb9a04e3902d34a
正文到此结束
Loading...