1、为什么使用@SpringBootApplication注解,即可做到自动配置?

答:@SpringBootApplication,内部起作用的注解其实有3个。@EnableAutoConfiguration,@ComponentScan,@Configuration。这篇文章主要是讲解@EnableXX注解

2、为什么使用了@EnableAutoConfiguration。当使用了@ConfigurationProperties时,即可自动导入.yml 或者.properties里面的配置项?

答:在@EnableAutoConfiguration内部,使用了@Import注解。导入AutoConfigurationImportSelector帮助springBoot将符合条件的Configuration加载到IOC容器中。但是内部其实使用了SpringFactoriesLoader来完成。类似与java SPI的功能
,即从/META-INF/spring.factories加载配置

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration 

可以看到@Import中,其实是导入了一个AutoConfigurationImportSelector的类。最关键的是,该类实现了接口ImportSelector

public interface ImportSelector {
    /**
     * Select and return the names of which class(es) should be imported based on
     * the {@link AnnotationMetadata} of the importing @{@link Configuration} class.
     */
    String[] selectImports(AnnotationMetadata importingClassMetadata);

}

这是ImportSelector的描述,大概意思就是,该方法返回的Bean 会自动的被注入,被Spring所管理。

接着来看 AutoConfigurationImportSelectorselectImports 的实现

public String[] selectImports(AnnotationMetadata annotationMetadata) {
        if(!this.isEnabled(annotationMetadata)) {
            return NO_IMPORTS;
        } else {
            AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);
            AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
            List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
            configurations = this.removeDuplicates(configurations);
            Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
            this.checkExcludedClasses(configurations, exclusions);
            configurations.removeAll(exclusions);
            configurations = this.filter(configurations, autoConfigurationMetadata);
            this.fireAutoConfigurationImportEvents(configurations, exclusions);
            return StringUtils.toStringArray(configurations);
        }
    }

代码都写得很清楚。就不解释了。

在@Import中,可以看到 有个链接指向了 ImportBeanDefinitionRegistrar。这同样是一个接口,作用跟 ImportSelector 一样。

public interface ImportBeanDefinitionRegistrar {
    public void registerBeanDefinitions(
            AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry);

}

在registerBeanDefinitions方法中,可以用BeanDefinitionRegistry 注入我们想要注入的Bean。

代码示例:
使用@Import编写自己的@Enable

1、创建2个测试Bean

public class Role {
}

public class User {
}

2、自定义Enable注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(MyEnableAutoConfig.class)
public @interface EnableBean {
}

3、实现自己的EnableAutoConfiguration类

public class MyEnableAutoConfig implements ImportSelector{
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{"com.xhn2.Role","com.xhn2.User"};
    }
}

4、编写启动类

@EnableBean
@ComponentScan("com.xhn2")
public class Main {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Main.class, args);
        System.out.println(context.getBean(User.class));
        System.out.println(context.getBean(Role.class));
    }
}

5、运行结果

com.xhn2.User@496bc455
com.xhn2.Role@59402b8f

控制台成功打印。

代码示例2,自动装配第3方jar包的Bean
新建maven工程

1、pom.xml

<modelVersion>4.0.0</modelVersion>

    <groupId>org.csp</groupId>
    <artifactId>hello</artifactId>
    <version>1.0.0</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.17.RELEASE</version>
        </dependency>
    </dependencies>

2、编写Configuration

@Configuration
public class MyTest {
    @Bean
    public Runnable runnable() {
        return ()->{};
    }
}

在resources下新建META-INF/spring.factories文件,加入以下配置

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.edu.MyTest

3、将项目安装到本地maven仓库:mvn install

4、主工程引入刚才安装到本地的jar。

<dependency>
            <groupId>org.csp</groupId>
            <artifactId>hello</artifactId>
            <version>1.0.0</version>
        </dependency>

5、获取刚才配置的Runnable

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Main.class);
        ConfigurableApplicationContext context = application.run(args);
        System.out.println(context.getBean(Runnable.class));
    }
}

6、控制台打印

com.edu.MyTest$$Lambda$153/284686302@2c07545f

心无私天地宽
513 声望22 粉丝