@SpringBootApplication 注解及源码 详解

@SpringBootApplication(scanBasePackages = {"com.XXX"}) 标注 启动类注解。

java 复制代码
内含源码注解
@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}
)}
)

前面四个不用说,是定义一个注解所必须的。

关键就在于后面三个注解:

@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan。

也就是说我们如果不用 @SpringBootApplication 这个复合注解,而是直接使用最下面这三个注解,也能启动一个 SpringBoot 应用。

@SpringBootConfiguration

java 复制代码
内含源码注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration

实际上就是一个 @Configuration 注解。

java 复制代码
@Configuration 内含源码注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component

@Configuration注解本质上是一个@Component注解,所以,被@Configuration注解标注的配置类本身也会被注册到IOC容器中。同时,@Configuration注解也会被@ComponentScan注解扫描到。

当某个类被@Configuration注解标注时,说明这个类是配置类,可以在这个类中使用@Bean注解向IOC容器中注入Bean对象,也可以使用@Autowired、@Inject和@Resource等注解来注入所需的Bean对象。

@ComponentScan

java 复制代码
内含源码注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Repeatable(ComponentScans.class)

用于定义 Spring 的扫描路径,等价于在 xml 文件中配置 context:component-scan,假如不配置扫描路径,那么 Spring 就会默认扫描当前类所在的包及其子包中的所有标注了 @Component,@Service,@Controller 等注解的类。

@EnableAutoConfiguration(SpringBoot自动配置的核心)

java 复制代码
内含源码注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})

由 @AutoConfigurationPackage 和 @Import 注解组成的复合注解。

1.@Import 注解其实就是为了去导入一个类AutoConfigurationImportSelector

2.当我们启动 SpringBoot 项目的时候,SpringBoot 会扫描所有 jar 包下面的 META-INF/spring.factories 文件。 Spring 默认就会去读取这些文件中的类并将这些类实例化注册成容器的 Bean。

java 复制代码
/**
 * 获取候选自动配置类列表。
 * 首先从 META-INF/spring.factories 文件中加载自动配置类。
 * 然后从 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 文件中加载自动配置类。
 * 最后返回合并后的自动配置类列表。
 */
    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
        Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
        return configurations;

3.@AutoConfigurationPackage**是将添加该注解的类所在的package作为自动配置package进行管理。**也就是说当SpringBoot应用启动时默认会将启动类所在的package作为自动配置的package。

java 复制代码
@AutoConfigurationPackage

内含源码注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})

4.@AutoConfigurationPackage导入了一个 AutoConfigurationPackages 的内部类 Registrar, 而这个类其实作用就是读取到我们在最外层的 @SpringBootApplication 注解中配置的扫描路径(没有配置则默认当前包下),然后把扫描路径下面的类都加到数组中返回。

相关推荐
武子康几秒前
MultiNet 只听到一次命令,为什么连续触发?
人工智能·后端·agent
挖掘狂人5 分钟前
同一个端口能不能被两个程序同时监听?
java·后端·程序员
codigger9 分钟前
同一个端口能不能被两个程序同时监听?
java·后端·程序员·编程
西索斯coding9 分钟前
qwen3.8-max-0902 和 minimax-m3 改真实仓库差多少?2026 年 50 个缺陷复现实测框架
java·服务器·网络·ai
孤狼GPT14 分钟前
ChatGPT、Codex工程实战:Spring Boot的SSE一直在推送,为什么经过Nginx后前端才一次性显示?
spring boot·chatgpt·sse·codex·chatgptplus
2501_9339232517 分钟前
SpringCloud:OpenFeign快速实现
java·后端·spring cloud·openfeign
denlaku25 分钟前
JDK 26 新特性详解
java
SL_staff29 分钟前
3分钟完成业务规则调整:告别代码修改,实现拖拽式逻辑配置
java·低代码·全栈
拖孩30 分钟前
AI 帮我写的小程序上线 44 天:36 个人进了收银台,8 个人付钱
前端·后端·微信小程序
Flynn Liang33 分钟前
让 AI 查订单、查库存、发消息:Spring AI 自定义工具(Tool)实战与工具描述工程
java·人工智能·spring·函数调用·springai·tool·自定义工具