1.@SpringBootApplication

springboot采用注解方式开发的,当创建了一个springboot项目时,在启动类上会有一个注解@SpringBootApplication,这个注解用来标识是一个springboot的项目,并且此类是启动类。

进入@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 { /**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
@AliasFor(annotation = EnableAutoConfiguration.class)
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
*/
@AliasFor(annotation = EnableAutoConfiguration.class)
String[] excludeName() default {}; /**
* Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}
* for a type-safe alternative to String-based package names.
* @return base packages to scan
* @since 1.3.0
*/
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {}; /**
* Type-safe alternative to {@link #scanBasePackages} for specifying the packages to
* scan for annotated components. The package of each class specified will be scanned.
* <p>
* Consider creating a special no-op marker class or interface in each package that
* serves no purpose other than being referenced by this attribute.
* @return base packages to scan
* @since 1.3.0
*/
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {}; }

先看@Target:

2.一级注解

2.1@Target

作用是指定注解的使用范围。其源码如下:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
/**
* Returns an array of the kinds of elements an annotation type
* can be applied to.
* @return an array of the kinds of elements an annotation type
* can be applied to
*/
ElementType[] value();
}

在这个注解里面就定义了一个枚举类型的数组,其枚举类型如下:

public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE, /** Field declaration (includes enum constants) */
FIELD, /** Method declaration */
METHOD, /** Formal parameter declaration */
PARAMETER, /** Constructor declaration */
CONSTRUCTOR, /** Local variable declaration */
LOCAL_VARIABLE, /** Annotation type declaration */
ANNOTATION_TYPE, /** Package declaration */
PACKAGE, /**
* Type parameter declaration
*
* @since 1.8
*/
TYPE_PARAMETER, /**
* Use of a type
*
* @since 1.8
*/
TYPE_USE
}

表示的意义见表格:

类型 说明 用法
TYPE 表明此注解只能用于接口、类上

@Target(ElementType.TYPE)

 FIELD  表明此注解只能用于字段上  @Target(ElementType.FIELD)
 METHOD 表明此注解只能用于方法上   @Target(ElementType.METHOD) 
 PARAMETER 表明此注解只能用于方法参数上   @Target(ElementType.PARAMETER) 
 CONSTRUCTOR 表明此注解只能用于构造函数上   @Target(ElementType.CONSTRUCTOR) 
 LOCAL_VARIABLE 表明此注解只能用于局部变量上   @Target(ElementType.LOCAL_VARIABLE) 
 ANNOTATION_TYPE 表明此注解只能用于注解上   @Target(ElementType.ANNOTATION_TYPE) 
 PACKAGE 表明此注解只能用于包上   @Target(ElementType.PACKAGE) 
 TYPE_PARAMETER 表明此注解只能用于类型参数上   @Target(ElementType.TYPE_PARAMETER) 
 TYPE_USE 表明此注解只能用于任何类型上   @Target(ElementType.TYPE_USE) 

再回到上面的源码,@Target(ElementType.TYPE)也就指定了注解只能用于接口、类上。

2.2.@Retention

作用是指定注解的保留位置。其源码如下:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
/**
* Returns the retention policy.
* @return the retention policy
*/
RetentionPolicy value();
}

注解中定义了一个枚举,源码如下:

public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE, /**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
*/
CLASS, /**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}

其中

SOURCE:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;

CLASS:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期;

RUNTIME:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在,即在运行时也存在。

那么源码中的注解就表示在运行时此注解也存在。

2.3@Documented

作用是指定自定义的注解是否能随着被定义的java文件生成到JavaDoc文档当中。那么源码中的注解就表示注解会生成到JavaDoc文档中。

2.4@Inherited

作用是只有用在类上时,会被子类继承此自定义的注解。也就是说当@Inherited注解加在某个类A上时,假如类B继承了A,则B也会带上该注解。那么源码中的注解就表示定义的注解被继承时子类也有父类的这些注解。

2.5@SpringBootConfiguration

其继承自@Configuration,表明当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到spring容器中,并且实例名就是方法名。那么源码中的注解就表示当前的注解是一个SpringBoot的配置类。

2.6@EnableAutoConfiguration

作用是开启允许自动配置,这样spring会帮我们自动配置。主要源码如下:

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

红色的注解在二级注解中介绍。

2.7@ComponentScan

作用是定义扫描的路径并从中找出标识了需要装配的类自动装配到spring的bean容器中,简单的说,就是把指定的类交给spring来管理。会被自动装配的注解包括@Controller、@Service、@Component、@Repository等等。那么源码中的注解就是扫描启动类所在的包以及下面的包。

3.二级注解

3.1@Import

作用是给容器中导入一个组件。那么源码中的注解作用是将所有需要导入的组件以全类名的方式返回并自动导入所有符合自动配置条件的Bean定义并加载到IOC容器。

3.2@AutoConfigurationPackage

作用是自动配置包,源码如下:

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

@Import指定导入的组件由AutoConfigurationPackages.Registrar.class将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器。

Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作。

SpringBoot源码解析的更多相关文章

  1. 【spring-boot 源码解析】spring-boot 依赖管理梳理图

    在文章 [spring-boot 源码解析]spring-boot 依赖管理 中,我梳理了 spring-boot-build.spring-boot-parent.spring-boot-depen ...

  2. SpringBoot源码解析系列文章汇总

    相信我,你会收藏这篇文章的 本篇文章是这段时间撸出来的SpringBoot源码解析系列文章的汇总,当你使用SpringBoot不仅仅满足于基本使用时.或者出去面试被面试官虐了时.或者说想要深入了解一下 ...

  3. springboot源码解析-管中窥豹系列之总体结构(一)

    一.简介 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...

  4. springboot源码解析-管中窥豹系列之项目类型(二)

    一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...

  5. springboot源码解析-管中窥豹系列之Runner(三)

    一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...

  6. springboot源码解析-管中窥豹系列之Initializer(四)

    一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...

  7. springboot源码解析-管中窥豹系列之排序(五)

    一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...

  8. springboot源码解析-管中窥豹系列之aware(六)

    一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...

  9. springboot源码解析-管中窥豹系列之web服务器(七)

    一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...

  10. springboot源码解析-管中窥豹系列之BeanDefinition(八)

    一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...

随机推荐

  1. Codeforces Global Round 9 A. Sign Flipping

    题目链接:https://codeforces.com/contest/1375/problem/A 题意 给出一个大小为 $n$ 的数组 $a$,可以反转每个数的正负,要求: 至少有 $\frac{ ...

  2. 2019牛客多校 Round3

    Solved:3 Rank:105 治哥出题了 我感动哭了 A Graph Game (分块) 题意:1e5个点 2e5条边 s(x)表示与x点直接相邻的点集合 有两种操作 1种将按输入顺序的边第l条 ...

  3. Codeforces Round #652 (Div. 2) B. AccurateLee(思维)

    题意: 给你一个01字符串,现在你可以删除其中的一些子序列,要求如下:当遇到1 0的俩个连续子字符串后,可以删除其中的一个字符,现在要求把他删到尽量最短并且字典序最小,输出最后的字符串 题解: 刚开始 ...

  4. Codeforces Round #633 div2 A~C

    A. Filling Diamonds 题意:给你n个菱形方块,问能构成图示形状的有多少种 题解:自己画几个不难发现答案是n 代码: 1 #include <iostream> 2 #in ...

  5. 继承自List<T>的类通过NewtonJson的序列化问题

    什么问题? NewtonSoft.Json是我们最常用的Json组件库之一了.这里来讨论下使用NewtonSoft.Json序列化List<T>子类的情景.序列化使用了类JsonSeria ...

  6. 详解Go语言I/O多路复用netpoller模型

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 本文使用的go的源码15.7 可以从 Go 源码目录结构和对应代码文件了解 Go 在不同 ...

  7. smartbits国产版本minismb –快速安装上手指南

    Minismb测试仪表是复刻smartbits的国产版本,是一款专门用于测试智能路由器,网络交换机的性能和稳定性的软硬件相结合的工具.可以通过此工具测试任何ip网络设备的端口吞吐率,带宽,并发连接数和 ...

  8. LINUX - 随机数

    #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h& ...

  9. HEXO版本控制与持续集成

    主要解决了hexo发布文章的繁琐,以及本地资源丢失,更换电脑等情况的出现. 采用AppVeyor实现. 转自 https://formulahendry.github.io/2016/12/04/he ...

  10. Git使用疑问

    1.git操作是出现Username for 'https://github.com':的验证问题 Username for 'https://github.com': 输入的是github上的邮箱账 ...