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

2023-12-04 20:01

本文主要是介绍SpringBootApplication注解保姆级带你如何应对面试官,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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。

这篇关于SpringBootApplication注解保姆级带你如何应对面试官的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Centos7安装JDK1.8保姆版

工欲善其事,必先利其器。这句话同样适用于学习Java编程。在开始Java的学习旅程之前,我们必须首先配置好适合的开发环境。 通过事先准备好这些工具和配置,我们可以避免在学习过程中遇到因环境问题导致的代码异常或错误。一个稳定、高效的开发环境能够让我们更加专注于代码的学习和编写,提升学习效率,减少不必要的困扰和挫折感。因此,在学习Java之初,投入一些时间和精力来配置好开发环境是非常值得的。这将为我

【吊打面试官系列-Redis面试题】说说 Redis 哈希槽的概念?

大家好,我是锋哥。今天分享关于 【说说 Redis 哈希槽的概念?】面试题,希望对大家有帮助; 说说 Redis 哈希槽的概念? Redis 集群没有使用一致性 hash,而是引入了哈希槽的概念,Redis 集群有 16384 个哈希槽,每个 key 通过 CRC16 校验后对 16384 取模来决定放置哪个槽, 集群的每个节点负责一部分 hash 槽。

spring—使用注解配置Bean

从Spring2.5开始,出现了注解装配JavaBean的新方式。注解可以减少代码的开发量,spring提供了丰富的注解功能,现在项目中注解的方式使用的也越来越多了。   ** 开启注解扫描          Spring容器默认是禁用注解配置的。打开注解扫描的方式主要有两种: <context:component-scan>组件扫描和<context:annotation

面对Redis数据量庞大时的应对策略

面对Redis数据量庞大时的应对策略,我们可以从多个维度出发,包括数据分片、内存优化、持久化策略、使用集群、硬件升级、数据淘汰策略、以及数据结构选择等。以下是对这些策略的详细探讨: 一、数据分片(Sharding) 当Redis数据量持续增长,单个实例的处理能力可能达到瓶颈。此时,可以通过数据分片将数据分散存储到多个Redis实例中,以实现水平扩展。分片的主要策略包括: 一致性哈希:使用一

Jenkins+Svn+Vue自动化构建部署前端项目(保姆级图文教程)

目录 介绍 准备工作 配置jenkins 构建部署任务 常见问题 介绍 在平常开发前端vue项目时,我们通常需要将vue项目进行打包构建,将打包好的dist目录下的静态文件上传到服务器上,但是这种繁琐的操作是比较浪费时间的,可以使用jenkins进行自动化构建部署前端vue 准备工作 准备vue项目,服务器,linux,ubuntu,centos等都可以,服务器上已经

Spring Boot 注解探秘:HTTP 请求的魅力之旅

在SpringBoot应用开发中,处理Http请求是一项基础且重要的任务。Spring Boot通过提供一系列丰富的注解极大地简化了这一过程,使得定义请求处理器和路由变得更加直观与便捷。这些注解不仅帮助开发者清晰地定义不同类型的HTTP请求如何被处理,同时也提升了代码的可读性和维护性。 一、@RequestMapping @RequestMapping用于将特定的HTTP请求映射到特定的方法上

Redis缓存 自定义注解+aspect+反射技术实现

最近再给云随笔后台增加redis模块,突然发现spring-boot-starter-data-redis模块很不人性化,实现不了通用的方式,(当然,你也可以自己写个通用的CacheUtil来实现通用的方式),但由于本人非常的爱装逼,就在这里不讲解那种傻瓜式操作了,这里只讲干货,干到你不可置信的干货). 例如:这里我使用了它其中的RedisTemplate ,发现存到redis中后,数据

面试官:synchronized的锁升级过程是怎样的?

大家好,我是大明哥,一个专注「死磕 Java」系列创作的硬核程序员。 回答 在 JDK 1.6之前,synchronized 是一个重量级、效率比较低下的锁,但是在JDK 1.6后,JVM 为了提高锁的获取与释放效,,对 synchronized 进行了优化,引入了偏向锁和轻量级锁,至此,锁的状态有四种,级别由低到高依次为:无锁、偏向锁、轻量级锁、重量级锁。 锁升级就是无锁 —>

Mybatis注解用法

MyBatis(八) mybatis注解 一、mybatis简单注解 1、@Select、@Results、@Result2、@Delete、@Param、@ResultMap3、@Insert、@SelectKey4、@Delete、@Param5、@Update二、动态SQL 1、简单处理,直接使用``脚本2、使用Provider注解标识 2.1、创建Provider类2.2、注解使用Prov

springMVC 参数绑定的注解

本文介绍了用于参数绑定的相关注解。 绑定:将请求中的字段按照名字匹配的原则填入模型对象。 SpringMVC就跟Struts2一样,通过拦截器进行参数匹配。 代码在 https://github.com/morethink/MySpringMVC URI模板变量 这里指uri template中variable(路径变量),不含queryString部分 @PathVariable