【SpringBoot-注解】@SpringBootApplication

1.理解@SpringBootApplication

@SpringBootApplication被用于激活@EnableAutoConfiguration、@ComponentScan、@Configuration三个注解的特性。

其中:

  • @EnableAutoConfiguration 负责激活spring boot自动装配机制
  • @ComponentScan 负责激活@Component的扫描
  • @Configuration 负责声明被标注为配置类

可以说明@SpringBootApplication等同于@EnableAutoConfiguration、@ComponentScan、@Configuration

但是在2.0之后就有变化了

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}
)}
)
public @interface SpringBootApplication {}

@ComponentScan并没有使用默认值,而是添加了排除的TypeFilter实现:

java 复制代码
public class AutoConfigurationExcludeFilter implements TypeFilter, BeanClassLoaderAware
java 复制代码
public class TypeExcludeFilter implements TypeFilter, BeanFactoryAware {
  • TypeExcludeFilter:用于查找BeanFactory中已经注册的TypeExcludeFilterBean,作为贷利执行对象。

  • AutoConfigurationExcludeFilter:用于排除其他同时标注@Configuration和@EnableAutoConfiguration的类。

从SpringBoot1.4开始@SpringBootApplication注解就开始有变化了。@SpringBootApplication没有标注@Configuration,而改成了@SpringBootConfiguration,运行上没有差矣。

关系变为:

  • @SpringBootConfiguration
    • @Configuration
      • @Component

尽管@ComponentScan 关注与@Component,但是也看到@SpringBootConfiguration 属于多层级引用了@Component,所以也能够被scan到。

我们用到的@Service 、@Repository、@Controller都是属于 @Component的派生注解也叫Spring模式注解。

2.相关属性

java 复制代码
    @AliasFor(
        annotation = EnableAutoConfiguration.class
    )
    Class<?>[] exclude() default {};

    @AliasFor(
        annotation = EnableAutoConfiguration.class
    )
    String[] excludeName() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackages"
    )
    String[] scanBasePackages() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackageClasses"
    )
    Class<?>[] scanBasePackageClasses() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "nameGenerator"
    )
    Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

    @AliasFor(
        annotation = Configuration.class
    )
    boolean proxyBeanMethods() default true;

可以看到属性都标注@AliasFor注解,它就是用于桥接其他注解的属性。@AliasFor注解能够将一个或多个注解的属性 别名 于某个注解中。

所以综上所述 @SpringBootApplication 是一个聚合注解,被我们标注在引导类上。

相关推荐
zhanghe68714 分钟前
es的密码带有特殊字符,导出
java
pnoker25 分钟前
IoT DC3 概念解读:把设备抽象成一套语义模板——位号、指令、事件与面向智能体的设备建模
java·人工智能·物联网·iot·dc3
my_realmy26 分钟前
Java SE 基础学习笔记(一):面向对象、继承多态、抽象接口与异常(JDK 8)
java·多线程·并发··生产者消费者模式
天远数科1 小时前
风险治理实战:基于天远车信盟出险构建自动化理赔核保流水线
java·网络·人工智能·自动化
步行cgn1 小时前
YAML 的语法规则
spring boot·后端
骇客野人1 小时前
SpringBoot电商系统用户注册、登录、留存及数据埋点设计与落地实施方案
java·spring boot·后端
摇滚侠1 小时前
《SpringBoot 3:入门与应用实战》第 12 章 JDBC 与事务 数据库初始化机制 阅读笔记 36
数据库·spring boot·笔记
qiuhaipeng12 小时前
Claude code 升级后上下文长度变短问题
java·前端·数据库
wxwx_bscxy3222 小时前
基于Python新冠疫情可视化分析系统的设计与实现
java·数据库·spring boot·python·微信小程序·sqlite
evans在进步3 小时前
Tomcat NIO 工作原理详解:从 Acceptor、Poller 到容器处理链
java·tomcat·nio