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

相关推荐
leobertlan3 小时前
2025年终总结
前端·后端·程序员
面向Google编程3 小时前
从零学习Kafka:数据存储
后端·kafka
易安说AI4 小时前
Claude Opus 4.6 凌晨发布,我体验了一整晚,说说真实感受。
后端
易安说AI4 小时前
Ralph Loop 让Claude无止尽干活的牛马...
前端·后端
易安说AI4 小时前
用 Claude Code 远程分析生产日志,追踪 Claude Max 账户被封原因
后端
JH30735 小时前
SpringBoot 优雅处理金额格式化:拦截器+自定义注解方案
java·spring boot·spring
颜酱5 小时前
图结构完全解析:从基础概念到遍历实现
javascript·后端·算法
Coder_Boy_6 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
invicinble6 小时前
对tomcat的提供的功能与底层拓扑结构与实现机制的理解
java·tomcat
较真的菜鸟6 小时前
使用ASM和agent监控属性变化
java