@Override public String[] selectImports(AnnotationMetadata metadata) { if (!isEnabled()) { return new String[0]; } AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(this.annotationClass.getName(), true)); Assert.notNull(attributes, "No " + getSimpleName() + " attributes found. Is " + metadata.getClassName() + " annotated with @" + getSimpleName() + "?"); // Find all possible auto configuration classes, filtering duplicates List<String> factories = new ArrayList<>(new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(this.annotationClass, this.beanClassLoader))); if (factories.isEmpty() && !hasDefaultFactory()) { throw new IllegalStateException("Annotation @" + getSimpleName() + " found, but there are no implementations. Did you forget to include a starter?"); } if (factories.size() > 1) { // there should only ever be one DiscoveryClient, but there might be more than one factory LOG.warn("More than one implementation " + "of @" + getSimpleName() + " (now relying on @Conditionals to pick one): " + factories); } return factories.toArray(new String[factories.size()]); }
@Override public String[] selectImports(AnnotationMetadata metadata) { // 获取父类的Configuration列表 String[] imports = super.selectImports(metadata); // 从注解上获取参数 AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(getAnnotationClass().getName(), true)); boolean extension = attributes.getBoolean("extension"); if (extension) { // 如果EnableMyAnnotation注解上的extension为true,那么去装载MyConfigurationExtension,即初始化里面的MyBeanExtension List<String> importsList = new ArrayList<>(Arrays.asList(imports)); importsList.add(MyConfigurationExtension.class.getCanonicalName()); imports = importsList.toArray(new String[0]); } else { // 如果EnableMyAnnotation注解上的extension为false,那么你可以把该参数动态放到属性列表里 Environment environment = getEnvironment(); if (ConfigurableEnvironment.class.isInstance(environment)) { ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment; LinkedHashMap<String, Object> map = new LinkedHashMap<>(); map.put("extension.enabled", false); MapPropertySource propertySource = new MapPropertySource("nepxion", map); configurableEnvironment.getPropertySources().addLast(propertySource); } } return imports; }