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

相关文章

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

usaco 1.3 Prime Cryptarithm(简单哈希表暴搜剪枝)

思路: 1. 用一个 hash[ ] 数组存放输入的数字,令 hash[ tmp ]=1 。 2. 一个自定义函数 check( ) ,检查各位是否为输入的数字。 3. 暴搜。第一行数从 100到999,第二行数从 10到99。 4. 剪枝。 代码: /*ID: who jayLANG: C++TASK: crypt1*/#include<stdio.h>bool h

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直

【生成模型系列(初级)】嵌入(Embedding)方程——自然语言处理的数学灵魂【通俗理解】

【通俗理解】嵌入(Embedding)方程——自然语言处理的数学灵魂 关键词提炼 #嵌入方程 #自然语言处理 #词向量 #机器学习 #神经网络 #向量空间模型 #Siri #Google翻译 #AlexNet 第一节:嵌入方程的类比与核心概念【尽可能通俗】 嵌入方程可以被看作是自然语言处理中的“翻译机”,它将文本中的单词或短语转换成计算机能够理解的数学形式,即向量。 正如翻译机将一种语言

poj 1113 凸包+简单几何计算

题意: 给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。 解析: 韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。 用convexhull算法把凸包求出来,然后加加减减就A了。 计算见下图: 好久没玩画图了啊好开心。 代码: #include <iostream>#include <cstdio>#inclu

uva 10130 简单背包

题意: 背包和 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>

【C++高阶】C++类型转换全攻略:深入理解并高效应用

📝个人主页🌹:Eternity._ ⏩收录专栏⏪:C++ “ 登神长阶 ” 🤡往期回顾🤡:C++ 智能指针 🌹🌹期待您的关注 🌹🌹 ❀C++的类型转换 📒1. C语言中的类型转换📚2. C++强制类型转换⛰️static_cast🌞reinterpret_cast⭐const_cast🍁dynamic_cast 📜3. C++强制类型转换的原因📝

深入理解RxJava:响应式编程的现代方式

在当今的软件开发世界中,异步编程和事件驱动的架构变得越来越重要。RxJava,作为响应式编程(Reactive Programming)的一个流行库,为Java和Android开发者提供了一种强大的方式来处理异步任务和事件流。本文将深入探讨RxJava的核心概念、优势以及如何在实际项目中应用它。 文章目录 💯 什么是RxJava?💯 响应式编程的优势💯 RxJava的核心概念