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 { } 复制代码
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) { } } 复制代码
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()); } } 复制代码