All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.nacos.spring.context.annotation.NacosBeanDefinitionRegistrar Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.nacos.spring.context.annotation;

import static com.alibaba.nacos.spring.util.NacosBeanUtils.GLOBAL_NACOS_PROPERTIES_BEAN_NAME;
import static com.alibaba.nacos.spring.util.NacosBeanUtils.invokeNacosPropertySourcePostProcessor;
import static com.alibaba.nacos.spring.util.NacosBeanUtils.registerGlobalNacosProperties;
import static com.alibaba.nacos.spring.util.NacosBeanUtils.registerNacosCommonBeans;
import static com.alibaba.nacos.spring.util.NacosBeanUtils.registerNacosConfigBeans;
import static com.alibaba.nacos.spring.util.NacosBeanUtils.registerNacosDiscoveryBeans;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

/**
 * Nacos Properties {@link ImportBeanDefinitionRegistrar BeanDefinition Registrar}
 *
 * @author Mercy
 * @see EnableNacos
 * @since 0.1.0
 */
public class NacosBeanDefinitionRegistrar
		implements ImportBeanDefinitionRegistrar, EnvironmentAware, BeanFactoryAware {

	private Environment environment;

	private BeanFactory beanFactory;

	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
			BeanDefinitionRegistry registry) {
		BeanDefinition annotationProcessor = BeanDefinitionBuilder
				.genericBeanDefinition(PropertySourcesPlaceholderConfigurer.class)
				.getBeanDefinition();
		registry.registerBeanDefinition(
				PropertySourcesPlaceholderConfigurer.class.getName(),
				annotationProcessor);

		AnnotationAttributes attributes = AnnotationAttributes
				.fromMap(importingClassMetadata
						.getAnnotationAttributes(EnableNacos.class.getName()));

		// Register Global Nacos Properties Bean
		registerGlobalNacosProperties(attributes, registry, environment,
				GLOBAL_NACOS_PROPERTIES_BEAN_NAME);
		// Register Nacos Annotation Beans
		registerNacosAnnotationBeans(registry);
		// Invoke NacosPropertySourcePostProcessor immediately
		// in order to enhance the precedence of @NacosPropertySource process
		invokeNacosPropertySourcePostProcessor(beanFactory);
	}

	@Override
	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}

	public void registerNacosAnnotationBeans(BeanDefinitionRegistry registry) {
		registerNacosCommonBeans(registry);
		registerNacosConfigBeans(registry, environment, beanFactory);
		registerNacosDiscoveryBeans(registry);
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy