View Javadoc
1   package org.springframework.osgi.atlassian;
2   
3   import org.springframework.beans.factory.config.BeanDefinitionHolder;
4   import org.springframework.beans.factory.config.DependencyDescriptor;
5   import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
6   
7   import java.util.HashSet;
8   import java.util.Set;
9   
10  /**
11   * An autowire candidate resolver that excludes unwanted beans to be autowired.
12   * In Spring 4.3, some internal beans (e.g. importRegistry which is of type Collection)
13   * may be autowired when there are matched property types.
14   *
15   * @since 4.6.0
16   */
17  public class ExcludableContextAnnotationAutowireCandidateResolver extends ContextAnnotationAutowireCandidateResolver {
18  
19      private Set<String> excludedBeanNames = new HashSet<>();
20  
21      public ExcludableContextAnnotationAutowireCandidateResolver() {
22          //importRegistry bean is created by ConfigurationClassPostProcessor to support injection for ImportAware classes.
23          //It will be autowired to actionMessages and actionErrors properties of xwork actions that implement ValidationAware interface
24          //as they share the same Collection interface. It is likely to affect many plugins.
25          excludedBeanNames.add("org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry");
26      }
27  
28      public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
29          if (excludedBeanNames.contains(bdHolder.getBeanName())) {
30              return false;
31          }
32  
33          return super.isAutowireCandidate(bdHolder, descriptor);
34      }
35  }