Springboot版本是1.5.release.java
Springboot中使用EnableAutoConfiguration注解,以下所示:spring
List-1数组
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(EnableAutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; /** * Exclude specific auto-configuration classes such that they will never be applied. * @return the classes to exclude */ Class<?>[] exclude() default {}; /** * Exclude specific auto-configuration class names such that they will never be * applied. * @return the class names to exclude * @since 1.3.0 */ String[] excludeName() default {}; }
主要是那个EnableAutoConfigurationImportSelector,继承了接口ImportSelector,方法String[] selectImports(AnnotationMetadata var1)返回的类都会注入到Sping容器中。springboot
咱们来实现一个本身的ImportSelector。app
图1ide
如图1所示,咱们定义本身的类SelectorA、SelectorB、SelectorC,定义MyImportSelector以下所示:post
List-2spa
import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata; public class MyImportSelector implements ImportSelector { //会将这个方法返回的数组,放到容器中 @Override public String[] selectImports(AnnotationMetadata annotationMetadata) { return new String[]{"com.mjduan.project.springbootlearn.selector.SelectorA", "com.mjduan.project.springbootlearn.selector.SelectorB"}; } }
以后定义本身的EnableMyX,以下List-33d
List-3code
import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Import({MyImportSelector.class}) public @interface EnableMyX { }
在main方法的类上加上注解EnableMyX,以下所示:
List-4
import com.mjduan.project.springbootlearn.enable.EnableEcho; import com.mjduan.project.springbootlearn.selector.EnableMyX; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableMyX public class SpringbootLearnApplication { public static void main(String[] args) { SpringApplication.run(SpringbootLearnApplication.class, args); } }
咱们经过BeanPostProcessor来验证SelectorA和SelectorB加入到容器中了,以下
List-5
import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.stereotype.Component; @Component public class MyImportSelectorBeanPostProcessor implements BeanPostProcessor { private String[] classess={ "com.mjduan.project.springbootlearn.selector.SelectorA", "com.mjduan.project.springbootlearn.selector.SelectorB"}; @Override public Object postProcessBeforeInitialization(Object o, String s) throws BeansException { return o; } @Override public Object postProcessAfterInitialization(Object o, String s) throws BeansException { for (String str : classess) { if (o.getClass().getName().equals(str)) { System.out.println(o.getClass().getName()); } } return o; } }
以后启动应用,在控制台就能够看到打印出的SelectorA和SelectorB了。
ImportSelector返回的String[]会被注入到Spring容器中,EnableAutoConfiguration中EnableAutoConfigurationImportSelector返回的是META-INF/spring.factories中org.springframework.boot.autoconfigure.EnableAutoConfiguration的值,这些值都是些类名,这些类上都有@Configuration注解,这样就将这个配置类中的bean加入到容器中。