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

相关推荐
呱牛do it10 分钟前
企业级门户网站设计与实现:基于SpringBoot + Vue3的全栈解决方案(Day 7)
java·vue
NE_STOP17 分钟前
Redis--SDS字符串与集合的底层实现原理
java
aLTttY20 分钟前
Spring Boot + Redis 实现接口防抖与限流实战指南
spring boot·redis·junit
直奔標竿21 分钟前
Java开发者AI转型第二十二课!Spring AI 个人知识库实战(一)——架构搭建与核心契约落地
java·人工智能·后端·spring·架构
身如柳絮随风扬23 分钟前
深入理解Java IO与NIO的区别:从BIO到NIO的演进
java·nio
清汤饺子28 分钟前
【译】我的 AI 进阶之路:从怀疑到深度整合
前端·javascript·后端
A-Jie-Y1 小时前
JAVA设计模式-抽象工厂模式
java·设计模式
@insist1231 小时前
信息安全工程师-密码学专题(下):构建可信网络空间的核心机制
java·大数据·密码学·软考·信息安全工程师·软件水平考试
无厚1 小时前
Spring Boot中LLM流式交互的核心原理
后端·设计
摇滚侠1 小时前
Java 零基础全套视频教程,面向对象(高级),笔记 105-120
java·开发语言·笔记