对@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

相关文章

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

redis群集简单部署过程

《redis群集简单部署过程》文章介绍了Redis,一个高性能的键值存储系统,其支持多种数据结构和命令,它还讨论了Redis的服务器端架构、数据存储和获取、协议和命令、高可用性方案、缓存机制以及监控和... 目录Redis介绍1. 基本概念2. 服务器端3. 存储和获取数据4. 协议和命令5. 高可用性6.

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

Idea实现接口的方法上无法添加@Override注解的解决方案

《Idea实现接口的方法上无法添加@Override注解的解决方案》文章介绍了在IDEA中实现接口方法时无法添加@Override注解的问题及其解决方法,主要步骤包括更改项目结构中的Languagel... 目录Idea实现接China编程口的方法上无法添加@javascriptOverride注解错误原因解决方

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav

Java中基于注解的代码生成工具MapStruct映射使用详解

《Java中基于注解的代码生成工具MapStruct映射使用详解》MapStruct作为一个基于注解的代码生成工具,为我们提供了一种更加优雅、高效的解决方案,本文主要为大家介绍了它的具体使用,感兴趣... 目录介绍优缺点优点缺点核心注解及详细使用语法说明@Mapper@Mapping@Mappings@Co

深入理解Apache Airflow 调度器(最新推荐)

《深入理解ApacheAirflow调度器(最新推荐)》ApacheAirflow调度器是数据管道管理系统的关键组件,负责编排dag中任务的执行,通过理解调度器的角色和工作方式,正确配置调度器,并... 目录什么是Airflow 调度器?Airflow 调度器工作机制配置Airflow调度器调优及优化建议最

Java中注解与元数据示例详解

《Java中注解与元数据示例详解》Java注解和元数据是编程中重要的概念,用于描述程序元素的属性和用途,:本文主要介绍Java中注解与元数据的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参... 目录一、引言二、元数据的概念2.1 定义2.2 作用三、Java 注解的基础3.1 注解的定义3.2 内

利用Python编写一个简单的聊天机器人

《利用Python编写一个简单的聊天机器人》这篇文章主要为大家详细介绍了如何利用Python编写一个简单的聊天机器人,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 使用 python 编写一个简单的聊天机器人可以从最基础的逻辑开始,然后逐步加入更复杂的功能。这里我们将先实现一个简单的