大厂面试真题:SpringBoot的核心注解

其实理解一个注解就行了@SpringBootApplication,我们的启动类其实就加了这一个

但是这么答也不行,因为面试官要的答案肯定不止这一个

我们打开SpringBootApplication的源码,会发现上面加了一堆的注解

相对而言比较重要是下面三个,但是ComponentScan这个是SpringContext里本身带的并不是SpringBoot引入的,这个注解的作用是

复制代码
扫描含有特定注解的类:@ComponentScan能够扫描指定包及其子包中所有使用@Component、@Service、@Repository、@Controller等注解的类,并将这些类实例化为Bean,注册到Spring容器中。这意味着开发者可以在需要的地方通过自动装配(如@Autowired)直接使用这些Bean,而无需手动创建。
扫描含有@Configuration的类:除了扫描注解Bean外,@ComponentScan还能扫描含有@Configuration的类,并使其配置生效。这允许开发者将配置类也纳入Spring容器的管理范围。

我们再解释一下其他两个注解的作用

(1)SpringBootConfiguration注解

@SpringBootConfiguration的代码如下

java 复制代码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Indexed
public @interface SpringBootConfiguration {
    @AliasFor(
        annotation = Configuration.class
    )
    boolean proxyBeanMethods() default true;
}

这个注解的其实主要的就是引入了一个Configuration的注解,SpringBoot启动类加SpringBootConfiguration这个的作用基本上等同于加了个@Configuration注解,表示当前SpringBoot的启动类也是一个配置类

(2)EnableAutoConfiguration注解

主要的代码逻辑如下

java 复制代码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

	/**
	 * Environment property that can be used to override when auto-configuration is
	 * enabled.
	 */
	String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

	/**
	 * Exclude specific auto-configuration classes such that they will never be applied.
	 * @return the classes to exclude
	 */
	Class<?>[] exclude() default {};

	/**
	 * Exclude specific auto-configuration class names such that they will never be
	 * applied.
	 * @return the class names to exclude
	 * @since 1.3.0
	 */
	String[] excludeName() default {};

}

我们重点看最后一个,它使用@Import注解导入了一个AutoConfigurationImportSelector.class

这个是SpringBoot实现自动配置的最重要的类,它用来加载classpath下spring.factories中所定义的自动配置类,将这些类自动加载为配置Bean

(3)ConditionalOnXXX系列注解

@ConditionalOn开头的注解在Spring Boot中非常常见,它们提供了一套丰富的条件化配置机制,允许开发者根据特定的条件来控制配置类或Bean的创建。这些注解基于@Conditional元注解实现,通过内部定义的Condition接口来判断条件是否满足

主要有以下几种:

1. @ConditionalOnBean

  • 作用:当指定的Bean存在时,条件成立,将创建当前Bean或激活当前配置类。
  1. @ConditionalOnMissingBean
  • 作用:当指定的Bean不存在时,条件成立,将创建当前Bean或激活当前配置类。
  1. @ConditionalOnClass
  • 作用:当类路径上存在指定类时,条件成立,将激活当前配置类。
  1. @ConditionalOnMissingClass
  • 作用:当类路径上不存在指定类时,条件成立,将激活当前配置类。
  1. @ConditionalOnProperty
  • 作用:当指定的配置属性具有特定的值时,条件成立,将创建当前Bean或激活当前配置类。
  1. @ConditionalOnExpression
  • 作用:当指定的SpEL(Spring Expression Language)表达式的结果为true时,条件成立,将创建当前Bean或激活当前配置类。

这个其实一共应该有14种,其他的不太常用。

相关推荐
IT 行者7 分钟前
Spring AI 2.0.0-M5 发布:全面转向 OpenAI Java SDK
java·人工智能·spring
Resky08188 分钟前
ReentrantReadWriteLock 深度解析
java·开发语言·juc
研究点啥好呢9 分钟前
Momenta后端开发面试题精选:10道高频考题+答案解析(数据产线方向)
c++·python·面试·求职招聘
铭keny10 分钟前
子系统 SSO 单点登录接入配置指南
java
电商API_1800790524730 分钟前
淘宝商品评论数据获取指南|批量自动化|api应用
java·爬虫·spring·性能优化·自动化
梦梦代码精33 分钟前
Likeshop一个开源商城到底有哪些功能模块?
java·低代码·开源·php
布局呆星35 分钟前
Spring Boot+MyBatis-Plus+Vue3前后端协作Note
spring boot·typescript·vue·mybatis
java1234_小锋40 分钟前
Spring AI 2.0 开发Java Agent智能体 - 对话与提示词工程(Prompt)
java·人工智能·spring
路在脚下@1 小时前
@PostConstruct、@PreDestroy 和 @DependsOn注解的使用和区别
spring boot
Frank_refuel1 小时前
C++之STL->string类的使用和实现
java·开发语言·c++