首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >玩转 Spring Boot 原理篇(核心注解知多少)

玩转 Spring Boot 原理篇(核心注解知多少)

作者头像
一猿小讲
发布2022-04-12 14:40:29
发布2022-04-12 14:40:29
38100
代码可运行
举报
文章被收录于专栏:一猿小讲一猿小讲
运行总次数:0
代码可运行

0.

0.0. 历史文章整理

玩转 Spring Boot 入门篇

玩转 Spring Boot 集成篇(MySQL、Druid、HikariCP)

玩转 Spring Boot 集成篇(MyBatis、JPA、事务支持)

玩转 Spring Boot 集成篇(Redis)

玩转 Spring Boot 集成篇(Actuator、Spring Boot Admin)

玩转 Spring Boot 集成篇(RabbitMQ)

玩转 Spring Boot 集成篇(@Scheduled、静态、动态定时任务)

玩转 Spring Boot 集成篇(任务动态管理代码篇)

玩转 Spring Boot 集成篇(定时任务框架Quartz)

玩转 Spring Boot 原理篇(源码环境搭建)

0.1. Spring Boot 核心注解知多少?

在《架构真经》开篇就提到了大道至简、分而治之,感觉 Spring Boot 的设计个人感觉也有点类似。

其实,伴随业务的发展,系统的架构又何尝不是周而复始的经历着万物之始,大道至简,分而治之,衍化至繁的重构迭代的一个过程呢。

那么,从全局上一览 Spring Boot 核心注解设计,会有何感想?

上图是结合 Spring Boot 源码梳理的一些核心注解整理,从表现形式能够体会到 Spring Boot 的一种高度封装的思想,一个 @SpringBootApplication 注解背后隐藏了诸多注解,Spring Boot 着实让程序员简化了配置,可以直面业务研发。

1. 万物之始,大道至简

学习 Spring Boot 的核心注解,就从应用程序入口处配置的 @SpringBootApplication 注解着手。

1.1. @SpringBootApplication

代码语言:javascript
代码运行次数:0
运行
复制
@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 {
    // ... ...
}

通过源代码可以看出,@SpringBootApplication 注解内部是包含 @SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan 等 3 个重要的注解的一个复合注解,功能上可谓是一箭(注解) N 雕(功能)。

2. 分而治之

2.1 @SpringBootConfiguration

代码语言:javascript
代码运行次数:0
运行
复制
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Indexed
public @interface SpringBootConfiguration {
    // ... ...
}

该注解标明该类使用 Spring 基于 JavaConfig 的注解,通过源代码可以看到 SpringBootConfiguration 注解是对 @Configuration 进行的封装,而 @Configuration 主要用于定义配置类,可以替换 xml 配置文件(想想之前的<beans>...</beans>),被注解的类内部包含有一个或多个被 @Bean 注解的方法,这些方法将会被扫描,并用于构建 Bean 定义,初始化 Spring 容器。

2.2 @ComponentScan

代码语言:javascript
代码运行次数:0
运行
复制
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
    // ... ...
}

@ComponentScan 该注解用来启动组件扫描,比如@Controller 注解、@Service 注解等,都可以被 @ComponentScan 注解扫描到。

2.3 @EnableAutoConfiguration

代码语言:javascript
代码运行次数:0
运行
复制
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    // ... ...
}

@EnableAutoConfiguration 是 Spring Boot 自动装配的关键,可以开启自动配置的功能(有关自动装配的原理,后面会单独去剖析)。

代码语言:javascript
代码运行次数:0
运行
复制
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage // 自动配置包
@Import(AutoConfigurationImportSelector.class) // 作用是导入需要自动配置的组件
public @interface EnableAutoConfiguration {

  /**
   * Environment property that can be used to override when auto-configuration is
   * enabled.
   */
  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 {};

}

从 @EnableAutoConfiguration 源代码可以看出,其内部引入了 @AutoConfigurationPackage 和 @Import 这两个注解。@AutoConfigurationPackage 的作用是自动配置包;@Import 则是导入需要自动配置的组件。

回头瞅瞅,Spring Boot 对 @SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan 等重要的注解的配置做了简化封装,在代码开发时只需 @SpringBootApplication 这一个复合注解就轻松开启 Spring 组件扫描和 Spring Boot 自动配置的功能,可谓是化繁为简,So Cool~。

3. 例行回顾

本文基于源码对 Spring Boot 的核心注解设计大概了解,重在体会 Spring Boot 架构的设计思想。

文末提到了 @EnableAutoConfiguration 这个 Spring Boot 自动装配的功臣,哪么 Spring Boot 自动装配是如何实现的呢?下次我们将顺着主线往下捋,感兴趣的可以顺着我画的这个流程图先自己体会体会自动装配的思想。

一起聊技术、谈业务、喷架构,少走弯路,不踩大坑,会持续输出更多精彩分享,敬请期待!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-03-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 一猿小讲 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档