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

com.ctrip.framework.apollo.spring.util.BeanRegistrationUtil Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
package com.ctrip.framework.apollo.spring.util;

import java.util.Objects;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;

/**
 * @author Jason Song([email protected])
 */
public class BeanRegistrationUtil {
  public static boolean registerBeanDefinitionIfNotExists(BeanDefinitionRegistry registry, String beanName,
                                                          Class beanClass) {
    if (registry.containsBeanDefinition(beanName)) {
      return false;
    }

    String[] candidates = registry.getBeanDefinitionNames();

    for (String candidate : candidates) {
      BeanDefinition beanDefinition = registry.getBeanDefinition(candidate);
      if (Objects.equals(beanDefinition.getBeanClassName(), beanClass.getName())) {
        return false;
      }
    }

    BeanDefinition annotationProcessor = BeanDefinitionBuilder.genericBeanDefinition(beanClass).getBeanDefinition();
    registry.registerBeanDefinition(beanName, annotationProcessor);

    return true;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy