注解

@SpringBootApplication 表示一个配置类:它声明一个或多个@Bean 方法,同时触发自动配置和组件扫描。这是一个组合注解相当于声明了@Configuration、@EnableAutoConfiguration 和@ComponentScan
image.png
@SoringBootApplication 三个注解

@interface 准确的说它不是一个接口,而是一个新的注释类型-注释类,它的类名就是注释名

@Target

  1. @Documented
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target(ElementType.ANNOTATION_TYPE)
  4. public @interface Target {
  5. /**
  6. * Returns an array of the kinds of elements an annotation type
  7. * can be applied to.
  8. * @return an array of the kinds of elements an annotation type
  9. * can be applied to
  10. */
  11. ElementType[] value();
  12. }

官方文档注释:
image.png
@Target 官方注释

它的意思是说,用了@Target 注解的注解,可以被用在哪些作用域中,有哪些作用域需要到java.lang.annotation.ElementType 里面去找

  1. public enum ElementType {
  2. /** 用在描述类、接口(包括注解类型)或枚举 */
  3. TYPE,
  4. /** 用在字段声明(包括枚举) */
  5. FIELD,
  6. /** 用于方法上 */
  7. METHOD,
  8. /** 用在参数上 */
  9. PARAMETER,
  10. /** 用在构造器(又叫构造方法)上 */
  11. CONSTRUCTOR,
  12. /** 用在局部变量上 */
  13. LOCAL_VARIABLE,
  14. /** 用在描述注释上 */
  15. ANNOTATION_TYPE,
  16. /** 用在包上 */
  17. PACKAGE,
  18. /** 从1.8开始支持,自定义类型参数上 */
  19. TYPE_PARAMETER,
  20. /** 从1.8开始支持,对类型注解 */
  21. TYPE_USE
  22. }

@Retention

  1. /**
  2. * Indicates how long annotations with the annotated type are to
  3. * be retained. If no Retention annotation is present on
  4. * an annotation type declaration, the retention policy defaults to
  5. * {@code RetentionPolicy.CLASS}.
  6. *
  7. * <p>A Retention meta-annotation has effect only if the
  8. * meta-annotated type is used directly for annotation. It has no
  9. * effect if the meta-annotated type is used as a member type in
  10. * another annotation type.
  11. *
  12. * @author Joshua Bloch
  13. * @since 1.5
  14. * @jls 9.6.3.2 @Retention
  15. */
  16. @Documented
  17. @Retention(RetentionPolicy.RUNTIME)
  18. @Target(ElementType.ANNOTATION_TYPE)
  19. public @interface Retention {
  20. /**
  21. * Returns the retention policy.
  22. * @return the retention policy
  23. */
  24. RetentionPolicy value();
  25. }

官方文档注释:
image.png@Retention 官方注释

@Retention 是定义该注解的生命周期有多久 ,而决定它的生命周期在一个枚举类型的RetentionPolicy里

image.png
RetentionPolicy

  • SOURCE:在源文件中有效(即源文件保留),编译器直接丢弃这种策略的注释,在.class文件中不会保留注解信息
  • CLASS:在class文件中有效(即class保留),保留在.class文件中,但是当运行Java程序时,他就不会继续加载了,不会保留在内存中,JVM不会保留注解。如果注解没有加Retention元注解,那么相当于默认的注解就是这种状态。
  • RUNTIME:在运行时有效(即运行时保留),当运行 Java程序时,JVM会保留注释,加载在内存中了,那么程序可以通过反射获取该注释。

@Documented (很少)

  1. /**
  2. * Indicates that annotations with a type are to be documented by javadoc
  3. * and similar tools by default. This type should be used to annotate the
  4. * declarations of types whose annotations affect the use of annotated
  5. * elements by their clients. If a type declaration is annotated with
  6. * Documented, its annotations become part of the public API
  7. * of the annotated elements.
  8. *
  9. * @author Joshua Bloch
  10. * @since 1.5
  11. */
  12. @Documented
  13. @Retention(RetentionPolicy.RUNTIME)
  14. @Target(ElementType.ANNOTATION_TYPE)
  15. public @interface Documented {
  16. }

官方文档注释:
image.png
@Documented 官方注释

用于指定被该元注解修饰的注解类将被javadoc工具提取成文档。默认情况下,javadoc是 不包括注解的,但是加上了这个注解生成的文档中就会带着注解了。 @Documented 注释只是用来生成文档的,不重要

@Inherited(极少)

  1. /**
  2. * Indicates that an annotation type is automatically inherited. If
  3. * an Inherited meta-annotation is present on an annotation type
  4. * declaration, and the user queries the annotation type on a class
  5. * declaration, and the class declaration has no annotation for this type,
  6. * then the class's superclass will automatically be queried for the
  7. * annotation type. This process will be repeated until an annotation for this
  8. * type is found, or the top of the class hierarchy (Object)
  9. * is reached. If no superclass has an annotation for this type, then
  10. * the query will indicate that the class in question has no such annotation.
  11. *
  12. * <p>Note that this meta-annotation type has no effect if the annotated
  13. * type is used to annotate anything other than a class. Note also
  14. * that this meta-annotation only causes annotations to be inherited
  15. * from superclasses; annotations on implemented interfaces have no
  16. * effect.
  17. *
  18. * @author Joshua Bloch
  19. * @since 1.5
  20. * @jls 9.6.3.3 @Inherited
  21. */
  22. @Documented
  23. @Retention(RetentionPolicy.RUNTIME)
  24. @Target(ElementType.ANNOTATION_TYPE)
  25. public @interface Inherited {
  26. }

官方文档注释:
image.png
@Inherited 官方注释

被它修饰的Annotation将具有继承性。如果某个类使用了被@Inherited修饰的Annotation,则其子类将自动具有该注解。

自动装配原理

  • 自动配置好Tomcat

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-tomcat</artifactId>
    4. <version>2.6.6</version>
    5. <scope>compile</scope>
    6. </dependency>
  • 自动配置好SpringMVC

    1. <dependency>
    2. <groupId>org.springframework</groupId>
    3. <artifactId>spring-webmvc</artifactId>
    4. <version>5.3.18</version>
    5. <scope>compile</scope>
    6. </dependency>
  • 自动配置好Web常见功能:

image.png
查看IOC容器组件
image.png
image.png
image.png

  • 默认的包结构(约定大于配置)

image.png
PS:可以扩大自动扫包的扩大范围
image.png

  • 各种配置拥有默认值
    • 配置文件的值最终会绑定每个类上,这个类会在容器中创建对象
  • 按需加载所有自动配置项
    • 非常多的starter
    • 引入了哪些场景这个场景的自动配置才会开启
    • SpringBoot所有的自动配置功能都在 spring-boot-autoconfigure

image.png

组件添加

@Configuration

  1. 配置类里面使用@Bean标注在方法上给容器注册组件,默认也是单实例的
  2. 配置类本身也是组件
  3. proxyBeanMethods:代理bean的方法
    • Full(proxyBeanMethods = true)、【保证每个@Bean方法被调用多少次返回的组件都是单实例的】
    • Lite(proxyBeanMethods = false)【每个@Bean方法被调用多少次返回的组件都是新创建的】
    • 组件依赖必须使用Full模式默认。其他默认是否Lite模式

image.png
image.png


自动配置原理

引导加载自动配置类

  1. @SpringBootConfiguration
  2. @EnableAutoConfiguration
  3. @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
  4. @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
  5. public @interface SpringBootApplication{
  6. }

1、@SpringBootConfiguration

@Configuration:代表当前是一个配置类
image.png

2、@ComponentScan

指定扫描哪些,Spring注解;

3、@EnableAutoConfiguration

image.png
@AutoConfigurationPackage
image.png
image.png
@Import(AutoConfigurationImportSelector.class)
image.png
image.png
image.png
image.png

按需开启自动配置项

image.png

修改默认配置

给容器中加入了文件上传解析器:传入的名称不叫multipartResolver,通过multipartResolver方法给它返回正确的名称

  1. @Bean
  2. @ConditionalOnBean(MultipartResolver.class) //容器中有这个类型组件
  3. // 容器中没有这个名字 multipartResolver 的组件
  4. @ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
  5. public MultipartResolver multipartResolver(MultipartResolver resolver) {
  6. //给@Bean标注的方法传入了对象参数,这个参数的值就会从容器中找。
  7. //SpringMVC multipartResolver。防止有些用户配置的文件上传解析器不符合规范
  8. // Detect if the user has created a MultipartResolver but named it incorrectly
  9. return resolver;
  10. }