Spring Import

分析

  • org.springframework.context.annotation.Import
  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface Import {
  5. /**
  6. * {@link Configuration @Configuration}, {@link ImportSelector},
  7. * {@link ImportBeanDefinitionRegistrar}, or regular component classes to import.
  8. *
  9. * 需要导入的类
  10. */
  11. Class<?>[] value();
  12. }

ImportBeanDefinitionRegistrar

  • 注册 Import Bean
  • org.springframework.context.annotation.ImportBeanDefinitionRegistrar
  1. public interface ImportBeanDefinitionRegistrar {
  2. /**
  3. * Register bean definitions as necessary based on the given annotation metadata of
  4. * the importing {@code @Configuration} class.
  5. * <p>Note that {@link BeanDefinitionRegistryPostProcessor} types may <em>not</em> be
  6. * registered here, due to lifecycle constraints related to {@code @Configuration}
  7. * class processing.
  8. *
  9. * 对import value属性的注册
  10. * @param importingClassMetadata annotation metadata of the importing class
  11. * @param registry current bean definition registry
  12. */
  13. void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry);
  14. }
  • 两个实现类
    1. org.springframework.context.annotation.AutoProxyRegistrar
    2. org.springframework.context.annotation.AspectJAutoProxyRegistrar

AutoProxyRegistrar

  1. public class AutoProxyRegistrar implements ImportBeanDefinitionRegistrar {
  2. private final Log logger = LogFactory.getLog(getClass());
  3. /**
  4. * 注册import bean定义
  5. */
  6. @Override
  7. public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
  8. boolean candidateFound = false;
  9. // 获取注解
  10. Set<String> annTypes = importingClassMetadata.getAnnotationTypes();
  11. for (String annType : annTypes) {
  12. // 注解属性
  13. AnnotationAttributes candidate = AnnotationConfigUtils.attributesFor(importingClassMetadata, annType);
  14. if (candidate == null) {
  15. continue;
  16. }
  17. // 获取 mode 属性
  18. Object mode = candidate.get("mode");
  19. // 获取代理对象
  20. Object proxyTargetClass = candidate.get("proxyTargetClass");
  21. if (mode != null && proxyTargetClass != null && AdviceMode.class == mode.getClass() &&
  22. Boolean.class == proxyTargetClass.getClass()) {
  23. candidateFound = true;
  24. if (mode == AdviceMode.PROXY) {
  25. // 注册
  26. AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry);
  27. if ((Boolean) proxyTargetClass) {
  28. AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
  29. return;
  30. }
  31. }
  32. }
  33. }
  34. if (!candidateFound && logger.isInfoEnabled()) {
  35. String name = getClass().getSimpleName();
  36. logger.info(String.format("%s was imported but no annotations were found " +
  37. "having both 'mode' and 'proxyTargetClass' attributes of type " +
  38. "AdviceMode and boolean respectively. This means that auto proxy " +
  39. "creator registration and configuration may not have occurred as " +
  40. "intended, and components may not be proxied as expected. Check to " +
  41. "ensure that %s has been @Import'ed on the same class where these " +
  42. "annotations are declared; otherwise remove the import of %s " +
  43. "altogether.", name, name, name));
  44. }
  45. }
  46. }