大厂面试真题: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种,其他的不太常用。

相关推荐
漫随流水3 分钟前
IDEA快速生成构造方法(空参、带参)
java·intellij-idea
spencer_tseng19 分钟前
Spring Boot 3.0+ jakarta.*
java·spring boot
Bat U27 分钟前
JavaEE|文件操作和IO
java·开发语言
DavidSoCool1 小时前
Spring AI Alibaba ReactAgent 调用Tool 实现多轮对话
java·人工智能·spring·多轮对话·reactagent
PRINT!1 小时前
个人财富全景管理系统 AssetMe【内容均为AI制作】
spring boot·信息可视化·ai编程
神所夸赞的夏天1 小时前
如何获取多层json数据,存成dictionary,并取最大最小值
java·前端·json
9号达人1 小时前
为什么你应该在 MQ 里用多个消费者,而不是一个
java·后端·架构
焦糖玛奇朵婷1 小时前
健身房预约小程序开发、设计
java·大数据·服务器·前端·小程序
小新同学^O^1 小时前
简单学习 --> TCP协议
java·网络·tcp
Raink老师1 小时前
用100道题拿下你的算法面试(链表篇-4):合并 K 个有序链表
算法·链表·面试