SpringBootApplication注解保姆级带你如何应对面试官

news2024/10/1 1:29:53

SpringBootApplication注解保姆级带你如何应对面试官

一介绍

在这里插入图片描述
在Java Spring Boot框架中,@SpringBootApplication注解是一个组合注解,它由以下三个注解组成:@SpringBootConfiguration,@EnableAutoConfiguration和@ComponentScan。

这三个注解的加载顺序如下:
  1. @SpringBootConfiguration:这个注解用于标识该类是Spring Boot应用的配置类,它会被自动扫描并加载。通常,这个注解会放在主类上(即包含main方法的类)。

  2. @EnableAutoConfiguration:这个注解用于启用Spring Boot的自动配置机制。它会根据项目的依赖和配置来自动配置Spring应用上下文。这个注解通常会放在配置类上,以确保自动配置生效。

  3. @ComponentScan:这个注解用于指定Spring应用程序上下文中要扫描的包。它会自动扫描并注册被@Component、@Service、@Controller等注解标记的Bean。通常,这个注解会放在配置类上,以扫描并加载所有需要的组件。

总结起来,@SpringBootApplication注解的加载顺序是先加载@SpringBootConfiguration,然后@EnableAutoConfiguration,最后@ComponentScan。这个加载顺序确保了正确的配置和自动装配,以及将需要的组件加载到Spring应用程序上下文中。

@SpringBootApplication 是 Spring Boot 中的一个组合注解,它包含了多个常用的注解,其中包括 @SpringBootConfiguration@EnableAutoConfiguration@ComponentScan 等。这些注解的加载顺序如下:

  1. @Target(ElementType.TYPE):指定该注解只能用于类或接口上。

  2. @Retention(RetentionPolicy.RUNTIME):指定该注解保留到运行时,并可以通过反射获取。

  3. @Documented:指定该注解可以被 javadoc 工具记录。

  4. @Inherited:指定该注解可以被继承。

  5. @SpringBootConfiguration:该注解用于替换 Spring 的 @Configuration 注解,表明该类是 Spring Boot 应用的配置类。

  6. @EnableAutoConfiguration:该注解用于自动配置 Spring Boot 应用程序,并根据 classpath 中的 jar 包依赖自动配置应用程序。

  7. @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }):该注解用于扫描指定包中的 bean,并排除一些不需要的 bean。其中,excludeFilters 属性用于指定排除哪些 bean,这里通过 @Filter 注解指定了两个自定义的过滤器。

总的来说,这些注解的加载顺序并没有具体的规定,因为这些注解之间并没有先后依赖关系。但是,如果你想要自定义 Spring Boot 应用程序的配置或者排除一些不需要的组件,你应该将这些注解应用在合适的位置,并根据需要进行合理的组合和配置。

二,源码注解以及翻译

@SpringBootApplication

Indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration and @ComponentScan.

指示一个配置类,它声明一个或多个@Bean方法,并触发自动配置和组件扫描。这是一个方便的注释,相当于声明@Configuration, @EnableAutoConfiguration和@ComponentScan。

@SpringBootConfiguration

Indicates that a class provides Spring Boot application @Configuration. Can be used as an alternative to the Spring’s standard @Configuration annotation so that configuration can be found automatically (for example in tests). Application should only ever include one @SpringBootConfiguration and most idiomatic Spring Boot applications will inherit it from @SpringBootApplication.

指示一个类提供Spring Boot应用程序@Configuration。可以用作Spring的标准@Configuration注释的替代,以便可以自动找到配置(例如在测试中)。应用程序应该只包含一个@SpringBootConfiguration,大多数习惯的Spring引导应用程序将从@SpringBootApplication继承它。

@EnableAutoConfiguration

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 tomcat-embedded.jar on your classpath you are likely to want a TomcatServletWebServerFactory (unless you have defined your own ServletWebServerFactory bean).
When using @SpringBootApplication, the auto-configuration of the context is automatically enabled and adding this annotation has therefore no additional effect.
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 exclude() any configuration that you never want to apply (use excludeName() if you don’t have access to them). You can also exclude them via the spring.autoconfigure.exclude property. Auto-configuration is always applied after user-defined beans have been registered.
The package of the class that is annotated with @EnableAutoConfiguration, usually via @SpringBootApplication, has specific significance and is often used as a ‘default’. For example, it will be used when scanning for @Entity classes. It is generally recommended that you place @EnableAutoConfiguration (if you’re not using @SpringBootApplication) in a root package so that all sub-packages and classes can be searched.
Auto-configuration classes are regular Spring @Configuration beans. They are located using the SpringFactoriesLoader mechanism (keyed against this class). Generally auto-configuration beans are @Conditional beans (most often using @ConditionalOnClass and @ConditionalOnMissingBean annotations).

启用Spring应用程序上下文的自动配置,尝试猜测和配置您可能需要的bean。自动配置类通常基于您的类路径和您定义的bean来应用。例如,如果您的类路径中有tomcat-embedded.jar,那么您可能需要一个TomcatServletWebServerFactory(除非您已经定义了自己的ServletWebServerFactory bean)。

当使用@SpringBootApplication时,上下文的自动配置是自动启用的,因此添加这个注释没有额外的效果。

自动配置试图尽可能地智能,并且会随着您定义更多自己的配置而后退。您始终可以手动排除()任何您不想应用的配置(如果您没有访问权限,请使用excludeName())。你也可以通过spring.autoconfigure.exclude属性排除它们。自动配置总是在用户定义bean注册之后应用。

用@EnableAutoConfiguration注释的类包,通常通过@SpringBootApplication,具有特定的意义,通常被用作“默认”。例如,它将在扫描@Entity类时使用。通常建议将@EnableAutoConfiguration(如果不使用@SpringBootApplication)放在根包中,以便可以搜索所有子包和类。

自动配置类是常规的Spring @Configuration bean。它们是使用springfactoresloader机制定位的(与这个类相关)。通常自动配置bean是@条件bean(最常使用@ConditionalOnClass和@ConditionalOnMissingBean注释)。

ComponentScan

Configures component scanning directives for use with @Configuration classes. Provides support parallel with Spring XML’s context:component-scan element.
Either basePackageClasses or basePackages (or its alias 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.
Note that the context:component-scan element has an annotation-config attribute; however, this annotation does not. This is because in almost all cases when using @ComponentScan, default annotation config processing (e.g. processing @Autowired and friends) is assumed. Furthermore, when using AnnotationConfigApplicationContext, annotation config processors are always registered, meaning that any attempt to disable them at the @ComponentScan level would be ignored.
See @Configuration’s Javadoc for usage examples.

配置与@Configuration类一起使用的组件扫描指令。提供与Spring XML的context:component-scan元素并行的支持。

可以指定basepackageclass或basePackages(或其别名值)来定义要扫描的特定包。如果没有定义特定的包,则将从声明该注释的类的包中进行扫描。

注意,context:component-scan元素有一个annotation-config属性;但是,这个注释没有。这是因为在几乎所有使用@ComponentScan的情况下,默认的注释配置处理(例如处理@Autowired和friends)都是假设的。此外,当使用AnnotationConfigApplicationContext时,注释配置处理器总是被注册的,这意味着任何在@ComponentScan级别禁用它们的尝试都会被忽略。

有关使用示例,请参阅@Configuration的Javadoc。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1283230.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

绿色建筑革新,气膜球馆成为城市锻炼新热点

近年来,全民健身设施蓬勃发展,个性化球场设计逐渐取代了传统模式,其中备受欢迎的是新潮的绿色建筑——气膜室内球馆。这种创新的建筑形式不仅适用于传统的篮球、足球、羽毛球等常规运动,还可以容纳冰壶、滑雪等更为复杂的活动&…

MySQL入门实战:安装与配置基础

MySQL是一个开源的关系型数据库管理系统,由瑞典MySQL AB公司开发,目前已经被Sun Microsystems公司收购。MySQL是一个非常流行的数据库管理系统,它的特点是轻量级、高性能、易于使用和高度可扩展。 MySQL是一个基于客户端/服务器的系统&#…

浅谈基于能耗评价指标的医院智能配电能效管理分析

摘要:目的:为了更好地推进医院能源管理工作,主要利用空调系统分项能耗对上海7所三甲医院能源管理工作存在的问题进行分析。方法:Pearson系数被用作分析影响因素与医院总能耗的关联程度,再利用单位面积总能耗和空调系统…

基于Gradio的快速搭建AI算法可视化Web界面部署教程

基于Gradio的快速搭建AI算法可视化Web界面部署教程 1 Gradio简介1.1 视图框架1.2 Gradio介绍 2 功能模块2.1 概述2.2 模块介绍2.2.1 gradio.File() 上传文件2.2.2 gradio.Slider() 配置滑动条2.2.3 gr.Textbox() 文本框2.2.4 gradio.Dropdown() 建立下拉列表2.2.5 gradio.inter…

分享80个菜单导航JS特效,总有一款适合您

分享80个菜单导航JS特效,总有一款适合您 80个菜单导航JS特效下载链接:https://pan.baidu.com/s/1NgNc759Kg1of_8vR7kaj6A?pwd6666 提取码:6666 Python采集代码下载链接:采集代码.zip - 蓝奏云 学习知识费力气,…

面向对象及编程

C语言是一门面向过程的编程语言, C、Java这些编程语言都是面向对象。 分门别类:抽取事物的共性,将相似事物归纳为一个类别 面向的对象的核心概念: 类:就是一个事物的类别 |--属性 …

数据结构和算法专题---1、数据结构和算法概述

本文会系统性的介绍算法的概念、复杂度,后续会更新算法思想以及常见的失效算法、限流算法、调度算法、定时算法等,辅助大家快速学习算法和数据结构知识。 概念 数据结构 概述 数据结构是计算机存储、组织数据的方式。数据结构是指相互之间存在一种或…

Ubuntu之Sim2Real环境配置(坑居多)

不要一上来就复制哦,因为很多下面的步骤让我走了很多弯路,如果可能的话,我会重新整理再发出来 前提: 参考教程 Docs 创建工作空间(不用跟着操作,无用) 1.创建sim2real server container 1.尝试创建sim2r…

Python 解析JSON实现主机管理

JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它以易于阅读和编写的文本形式表示数据。JSON 是一种独立于编程语言的数据格式,因此在不同的编程语言中都有对应的解析器和生成器。JSON 格式的设计目标是易于理解、…

防火墙规则保存及自定义链

目录 防火墙规则保存 备份工具 iptables services 自定义链 自定义链实现方式 删除自定义链 重命名自定义链 防火墙规则保存 命令:iptables -save 工具:iptables services [rootlocalhost ~]# iptables-save > /opt/iptables.bak #将文件保存…

科普类软文怎么写才能提高用户接受度?媒介盒子分享

科普类软文以干货为主,可以给用户带来实用价值,但是相应会比较枯燥。如何才能把科普内容讲得专业又有趣,从而提高用户接受度呢?媒介盒子接下来就分享三大技巧: 一、 联系产品选题 科普类软文想要写好就需要做好选题&…

【数据结构】手撕排序NO.1

🔥博客主页: 小羊失眠啦. 🎥系列专栏:《C语言》 《数据结构》 《Linux》《Cpolar》 ❤️感谢大家点赞👍收藏⭐评论✍️ 文章目录 一、排序的概念及其运用1.1 排序的概念1.2 常见的算法排序 二、 冒泡排序三、直接插入排…

2023年「全球化新品牌」与品牌出海路径洞察分析

观点:全球化品牌是未来品牌发展的最优选择 什么是全球化品牌? •多市场:在全球多个市场均有业务布局,既包括传统市场,也包括新兴市场。 •全渠道:线上第三方平台品牌独立站社交网络线下实体店,从2C扩展到2B。 •本土…

SSM项目实战-前端-在Index.vue中展示第一页数据

1、util/request.js import axios from "axios";let request axios.create({baseURL: "http://localhost:8080",timeout: 50000 });export default request 2、api/schedule.js import request from "../util/request.js";export let getSchedu…

开关电源调试时,常见的10个问题:

1、变压器饱和 变压器饱和现象 在高压或低压输入下开机(包含轻载,重载,容性负载),输出短路,动态负载,高温等情况下,通过变压器(和开关管)的电流呈非线性增长,当出现此现象时,电流的…

SpringBoot2.x整合WebService实现远程接口调用

一、添加依赖 <!-- SpringBoot 2.4 以下版本--> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId> </dependency><dependency><groupId>org.apach…

java日历功能

java 日历功能 功能概述java代码打印结果 功能概述 输入年份和月份&#xff0c;打印该月份所有日期&#xff0c;头部信息为星期一至星期日 java代码 package com.java.core.demoTest; import java.util.Calendar; import java.util.Scanner;// 打印日历 public class Calend…

接口测试很难?1分钟带你入门接口自动化测试

1、什么是接口&#xff1f; 接口是连接前台和后台的桥梁&#xff0c;前台通过接口调用后端已完成的功能&#xff0c;而无需关注内部的实现细节。借助于接口&#xff0c;可以实现前后台分离&#xff0c;各自完成开发工作后&#xff0c;进行联调&#xff0c;提高工作效率 2、接…

navicate16 2059 plugin http could not be loaded

plugin http could not be loaded 乱码 library path http.dll 今天新装一台机子的navicate遇到这个问题。 查了半天都是说 caching_sha2_password’的解决办法。 然后是咋解决的呢&#xff0c;真是丢脸 由于我是直接从浏览器复制下来的ip&#xff0c;所以虽然我只复制了ip地…