【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 是一个聚合注解,被我们标注在引导类上。

相关推荐
圆山猫6 小时前
[Virtualization](四):Linux KVM/RISC-V 的 vCPU 运行路径
java·linux·risc-v
城管不管7 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划
IT小白杨7 小时前
从环境制备到自动化工作流:多账号运营的工程化架构拆解
java·经验分享·自动化·安全架构·指纹浏览器
豆瓣鸡7 小时前
算法日记 - Day3
java·开发语言·算法
萧瑟余晖8 小时前
Java深入解析篇九之NIO详解
java·网络·nio
mldong8 小时前
从 mldong 到 jeeflow:一个工作流引擎的独立进化
后端
The Chosen One9858 小时前
高进度算法模板速记(待完善)
java·前端·算法
陈随易8 小时前
MoonBit抓包模块pcap,查看电脑的每一次联网通信
前端·后端·程序员
Aaron - Wistron8 小时前
Web API C# (Furion版)带 单元测试
开发语言·后端·c#
卷无止境9 小时前
写代码这件事,到底该讲究点什么?
后端·python