View Javadoc
1   package com.atlassian.plugin.spring.scanner.runtime.impl.util;
2   
3   import org.springframework.beans.factory.config.BeanDefinition;
4   import org.springframework.beans.factory.support.BeanDefinitionRegistry;
5   
6   public class BeanDefinitionChecker {
7       /**
8        * copyPasta from spring-context:component-scan classes
9        *
10       * Check the given candidate's bean name, determining whether the corresponding
11       * bean definition needs to be registered or conflicts with an existing definition.
12       */
13      public static boolean needToRegister(String beanName, BeanDefinition beanDefinition, BeanDefinitionRegistry registry) throws IllegalStateException {
14          if (!registry.containsBeanDefinition(beanName)) {
15              return true;
16          }
17  
18          BeanDefinition existingDef = registry.getBeanDefinition(beanName);
19          BeanDefinition originatingDef = existingDef.getOriginatingBeanDefinition();
20          if (originatingDef != null) {
21              existingDef = originatingDef;
22          }
23          if (isCompatible(beanDefinition, existingDef)) {
24              return false;
25          }
26          throw new IllegalStateException("Annotation-specified bean name '" + beanName +
27                  "' for bean class [" + beanDefinition.getBeanClassName() + "] conflicts with existing, " +
28                  "non-compatible bean definition of same name and class [" + existingDef.getBeanClassName() + "]");
29      }
30  
31      /**
32       * copyPasta from spring-context:component-scan classes
33       *
34       * Determine whether the given new bean definition is compatible with
35       * the given existing bean definition.
36       * <p>The default implementation simply considers them as compatible
37       * when the bean class name matches.
38       */
39      public static boolean isCompatible(BeanDefinition newDefinition, BeanDefinition existingDefinition) {
40  
41          return (newDefinition.getBeanClassName().equals(existingDefinition.getBeanClassName()) ||
42                  newDefinition.equals(existingDefinition));
43      }
44  }