Talk NacosConfigAutoConfiguration

sequence

In this paper, we look NacosConfigAutoConfiguration

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 introduced 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 realized ImportBeanDefinitionRegistrar, BeanFactoryAware interfaces; it setBeanFactory method registered 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 inherited NacosConfigurationPropertiesBinder; its doBind method creates NacosPropertySource, then added to context.getEnvironment () getPropertySources (), after the adoption of the bind method Binder.get (context.getEnvironment ()) will be binding.

summary

NacosConfigAutoConfiguration introduced NacosConfigBootBeanDefinitionRegistrar; NacosConfigBootBeanDefinitionRegistrar achieved ImportBeanDefinitionRegistrar, BeanFactoryAware interface; a method which setBeanFactory registered nacosConfigurationPropertiesBinder

doc

Guess you like

Origin juejin.im/post/5d8cd9c46fb9a04e3902d34a