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加入到容器中。