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

相关推荐
qq_1851986913 分钟前
SpringBoot-五-AOT
java·spring boot·后端
王大大的刀18 分钟前
Spring AI 重试引起的 LLM 重复调用
java·人工智能
Heo24 分钟前
怎么保证缓存和数据库一致性?
前端·后端·面试
晓晓_za89866837 分钟前
Geo 优化服务 CI/CD 流水线搭建:源码自动构建、测试与灰度发布
运维·服务器·tcp/ip·spring·缓存·ci/cd
风流 少年41 分钟前
hutool
java·服务器·开发语言
H_oRIZoN_1 小时前
Linux入门DAY27(文件IO(系统调用)详解|open/read/write/lseek)
java·linux·服务器
延凡科技1 小时前
从 “人管机器“ 到 “数据管人“:智慧矿山综合管控落地实践
java·后端·struts
卓怡学长1 小时前
w180springboot基于Java的悠扬乐器管理
java·spring boot·mysql·spring·maven·intellij-idea
古法安卓1 小时前
Android-高版本 LMKD 源码解析:基于 PSI 的内存压力监控与查杀全流程
android·java·android studio
众人皆醒我独醉2 小时前
调和循环:kubectl apply 之后发生了什么
面试·llm·gpu