对@SpringBootApplication注解的一些简单理解

2023-12-28 21:18

本文主要是介绍对@SpringBootApplication注解的一些简单理解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

@SpringBootApplication
在这里插入图片描述
点进去这个注解看它的源码,如下:
在这里插入图片描述
接下来先分析这四个元注解

  • @Target:限定注解运用的场景
    • 此处是@Target(ElementType.TYPE):限定给一个类型进行注解,比如类、接口、枚举
    • 除此以外还有:
      ElementType.ANNOTATION_TYPE 可以给一个注解进行注解
      ElementType.CONSTRUCTOR 可以给构造方法进行注解
      ElementType.FIELD 可以给属性进行注解
      ElementType.LOCAL_VARIABLE 可以给局部变量进行注解
      ElementType.METHOD 可以给方法进行注解
      ElementType.PACKAGE 可以给一个包进行注解
      ElementType.PARAMETER 可以给一个方法内的参数进行注解
  • @Retention:该注解定义了这个注解存活的时间
    • 此处是@Retention(RetentionPolicy.RUNTIME),表明该注解可以保留到程序运行时,会被加载进JVM,运行时可以获得到他们,因此@SpringBootApplication可以在程序运行时被获取到,它的生存周期很长。常见的@Autowired注解就是运行期注解
    • RetentionPolicy.SOURCE 注解只在源码阶段保留,在编译器进行编译时它将被丢弃忽视。
      RetentionPolicy.CLASS 注解只被保留到编译进行的时候,它并不会被加载到 JVM 中。常见的编译期注解:@Override
  • @Documented:注解信息会被包含到Javadoc文档中
  • @Inherited:如果一个超类被 【【@Inherited 注解过】的注解】 进行注解的话,那么如果它的子类没有被任何注解应用的话,那么这个子类就继承了超类的注解

综上:@SpringBootApplication是一个被限定给类、接口进行注解的运行期注解,并且被@SpringBootApplication注解的类的子类没有被注解的话,子类可以继承该注解

除了上述四个注解,还有一个元注解:@Repeatable,JDK1.8新加入进来的,算是一个新特性,通常是注解的值可以同时取多个(比如一个Person身兼数职,既是学生又是兼职家教老师)


除去上面四个元注解,还有三个关键性的注解:
在这里插入图片描述
1.@SpringBootConfiguration:Spring Boot项目的配置注解

在这里插入图片描述
可以看到,@SpringBootConfiguration也是一个复合注解,但是前三个我们一句熟悉了,只有最后一个 @Configuration:它的作用是声明了这是一个配置类
在这里插入图片描述
2.@EnableAutoConfiguration:完成自动配置,该注解会使Spring Boot根据项目中依赖的jar包自动配置项目的配置项

 * Enable auto-configuration of the Spring Application Context, attempting to guess and* configure beans that you are likely to need. Auto-configuration classes are usually* applied based on your classpath and what beans you have defined. For example, if you* have {@code tomcat-embedded.jar} on your classpath you are likely to want a* {@link TomcatServletWebServerFactory} (unless you have defined your own* {@link ServletWebServerFactory} bean).* <p>* When using {@link SpringBootApplication}, the auto-configuration of the context is* automatically enabled and adding this annotation has therefore no additional effect.* <p>* Auto-configuration tries to be as intelligent as possible and will back-away as you* define more of your own configuration. You can always manually {@link #exclude()} any* configuration that you never want to apply (use {@link #excludeName()} if you don't* have access to them). You can also exclude them via the* {@code spring.autoconfigure.exclude} property. Auto-configuration is always applied* after user-defined beans have been registered.* <p>* The package of the class that is annotated with {@code @EnableAutoConfiguration},* usually via {@code @SpringBootApplication}, has specific significance and is often used* as a 'default'. For example, it will be used when scanning for {@code @Entity} classes.* It is generally recommended that you place {@code @EnableAutoConfiguration} (if you're* not using {@code @SpringBootApplication}) in a root package so that all sub-packages* and classes can be searched.* <p>* Auto-configuration classes are regular Spring {@link Configuration} beans. They are* located using the {@link SpringFactoriesLoader} mechanism (keyed against this class).* Generally auto-configuration beans are {@link Conditional @Conditional} beans (most* often using {@link ConditionalOnClass @ConditionalOnClass} and* {@link ConditionalOnMissingBean @ConditionalOnMissingBean} annotations).

在这里插入图片描述
public @interface ConditionalOnMissingBean{…}
在这里插入图片描述
@Inherited注解会超类来找依赖中的类,完成自动初始化类加载

3.@ComponentScan:用注解配置实现自动扫描,默认会扫描当前包和所有子包,和xml配置自动扫描效果一样,@Filter是排除了两个类

 * Configures component scanning directives for use with @{@link Configuration} classes.* Provides support parallel with Spring XML's {@code <context:component-scan>} element.** <p>Either {@link #basePackageClasses} or {@link #basePackages} (or its alias* {@link #value}) may be specified to define specific packages to scan. If specific* packages are not defined, scanning will occur from the package of the* class that declares this annotation.** <p>Note that the {@code <context:component-scan>} element has an* {@code annotation-config} attribute; however, this annotation does not. This is because* in almost all cases when using {@code @ComponentScan}, default annotation config* processing (e.g. processing {@code @Autowired} and friends) is assumed. Furthermore,* when using {@link AnnotationConfigApplicationContext}, annotation config processors are* always registered, meaning that any attempt to disable them at the* {@code @ComponentScan} level would be ignored.** <p>See {@link Configuration @Configuration}'s Javadoc for usage examples.

在这里插入图片描述
@ComponentScan 注解会自动扫描指定包下的全部标有 @Component注解 的类包括 @Component 下的子注解@Service、@Repository、@Controller等,并注册成bean

这篇关于对@SpringBootApplication注解的一些简单理解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/547334

相关文章

spring IOC的理解之原理和实现过程

《springIOC的理解之原理和实现过程》:本文主要介绍springIOC的理解之原理和实现过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、IoC 核心概念二、核心原理1. 容器架构2. 核心组件3. 工作流程三、关键实现机制1. Bean生命周期2.

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代

Java中的@SneakyThrows注解用法详解

《Java中的@SneakyThrows注解用法详解》:本文主要介绍Java中的@SneakyThrows注解用法的相关资料,Lombok的@SneakyThrows注解简化了Java方法中的异常... 目录前言一、@SneakyThrows 简介1.1 什么是 Lombok?二、@SneakyThrows

深入理解Apache Kafka(分布式流处理平台)

《深入理解ApacheKafka(分布式流处理平台)》ApacheKafka作为现代分布式系统中的核心中间件,为构建高吞吐量、低延迟的数据管道提供了强大支持,本文将深入探讨Kafka的核心概念、架构... 目录引言一、Apache Kafka概述1.1 什么是Kafka?1.2 Kafka的核心概念二、Ka

SpringRetry重试机制之@Retryable注解与重试策略详解

《SpringRetry重试机制之@Retryable注解与重试策略详解》本文将详细介绍SpringRetry的重试机制,特别是@Retryable注解的使用及各种重试策略的配置,帮助开发者构建更加健... 目录引言一、SpringRetry基础知识二、启用SpringRetry三、@Retryable注解

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

SpringBoot条件注解核心作用与使用场景详解

《SpringBoot条件注解核心作用与使用场景详解》SpringBoot的条件注解为开发者提供了强大的动态配置能力,理解其原理和适用场景是构建灵活、可扩展应用的关键,本文将系统梳理所有常用的条件注... 目录引言一、条件注解的核心机制二、SpringBoot内置条件注解详解1、@ConditionalOn

SpringBoot利用@Validated注解优雅实现参数校验

《SpringBoot利用@Validated注解优雅实现参数校验》在开发Web应用时,用户输入的合法性校验是保障系统稳定性的基础,​SpringBoot的@Validated注解提供了一种更优雅的解... 目录​一、为什么需要参数校验二、Validated 的核心用法​1. 基础校验2. php分组校验3