SpringBoot番外篇之@SpringBootApplication相关注解简单理解

版权声明:本文为FromTheWind原创文章,转载请注明出处。https://blog.csdn.net/FromTheWind/article/details/84502604

SpringBootApplication是一个组合注解,进入到application类中,按住Ctrl进入到@SpringBootApplication内部: 

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {        @Filter(
            type = FilterType.CUSTOM,
            classes = {TypeExcludeFilter.class}
        ),         @Filter(
            type = FilterType.CUSTOM,
            classes = {AutoConfigurationExcludeFilter.class}
        )}
)
public @interface SpringBootApplication {......}

从以上源码可以看到它组合了@SpringBootConfiguration、@EnableAutoConfiguration以及@ComponentScan。@SpringBootConfiguration内部使用了@Configuration注解,说明这是一个配置类。

@EnableAutoConfiguration则表示让Spring Boot根据类路径中的jar包依赖为当前项目进行自动配置。@Configuration将java类声明为一个配置类,等同于spring的xml文件。

@ComponentScan扫描注解,如:@Controller,@Service等注解

详解:

1、@EnableAutoConfiguration注解:

    spring通常建议我们将main方法所在的类放到一个root包下,@EnableAutoConfiguration(开启自动配置)注解通常都放到main所在类的上面,这样@EnableAutoConfiguration可以从逐层的往下搜索各个加注解的类,例如,你正在编写一个JPA程序(如果你的pom里进行了配置的话),spring会自动去搜索加了@Entity注解的类,并进行调用。

@EnableAutoConfiguration借助@Import的帮助,通过selectImports从META-INF/spring.factories中取出想要的配置,将所有符合自动配置条件(这里的条件时spring制定的,带有@Conditional注解就是加了条件,文章结尾又常用几个条件的解释)的bean定义加载到spring(IoC)容器。

从代码中分析(这里时Spring Boot 2.1.0.RELEASE版本):进入到@EnableAutoConfiguration中,看到@Import加载了AutoConfigurationImportSelector.class(这是重点),而在AutoConfigurationImportSelector中通过                             List<String> configurations=this.getCandidateConfigurations(annotationMetadata,attributes)获取了springboot自动配置相关类。从getCandidateConfigurations方法层层进入能看到spring-boot-autoconfigure.jar中的META-INF/spring.factories文件(以key=value形式存在)中以org.springframework.boot.autoconfigure.EnableAutoConfiguration为key的数据被EnableAutoConfiguration.class以反射的方式根据具体条件加载到spring容器

代码调用流程:AutoConfigurationImportSelector->selectImports(...)->this.getAutoConfigurationEntry(...)->this.getCandidateConfigurations(...)->SpringFactoriesLoader.loadFactoryNames(...)->factoryClass.getName()。

详情请查看参考文章:https://blog.csdn.net/u010399009/article/details/79290699

2、@Configuration注解:

      使用了@Configuration注解的java类为配置类。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

查看源代码发现@Configuration本质还是@Conponent。因此<context:component-scan/>或者@ComponentScan都能处理@Configuration注解的类。从网上的资料理解有的说@Configuration注解相当于xml文件的<beans>,有的说同@Bean(配合@Configuration使用在方法上)的功能一样,都等同于<bean>。个人觉得综合起来理解更为合适:@Configuration声明该java类为配置类,同时将该类配置为Bean;配置类内部使用了@Bean的方法,以返回值类型为class配置为Bean。

3、@ComponentScan

      Spring是一个依赖注入(dependency injection)框架。所有的内容都是关于bean的定义及其依赖关系。定义Spring Beans的第一步是使用正确的注解-@Component或@Service或@Repository。但是,Spring不知道你定义了某个bean。@ComponentScan做的事情就是告诉Spring从哪里找到bean。默认情况下会扫描使用了@ComponentScan的包及其子包。

4、拓展:

@component :(把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)。  泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用            @Component来标注这个类。

@Component 和 @ComponentScan 的区别:@Component 和 @ComponentScan的使用目的不一样。

  • 在某个类上使用@Component注解,表示将该类配置为Bean,交给spring来管理(IoC、DI)。
  • @ComponentScan 用于扫描指定包下的类。确定被spring管理的类都有哪些。

@Conditional常用条件注解说明:

    @ConditionalOnClass:当类路径下有指定类时实例

    @ConditionalOnMissingClass:某个class类路径上不存在时实例化

    @ConditionalOnProperty:指定属性存在指定值时实例化

    @ConditionalOnExpression:当表达式为true时实例化

    @ConditionalOnBean:仅仅在当前上下文中存在某个对象时实例化

    @ConditionalOnMissingBean:仅仅在当前上下文中不存在某个对象时实例化

    @ConditionalOnNotWebApplication:不是web应用时实例化

猜你喜欢

转载自blog.csdn.net/FromTheWind/article/details/84502604