java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot自动装配注解

SpringBoot自动装配注解的实现示例

作者:kevinzeng

本文主要介绍了SpringBoot自动装配注解的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

这份指南详细整理了 Spring Boot 中最核心的配置绑定自动装配以及条件注解。这些注解是理解 Spring Boot "约定大于配置" (Convention over Configuration) 机制的基石。

本文档详细解析 Spring Boot 中用于配置绑定、自动装配流程控制及条件加载的核心注解。

1. 配置属性绑定 (Configuration Binding)

这一组注解的主要作用是将 application.yml 或 application.properties 中的配置值绑定到 Java Bean 中。

@ConfigurationProperties

@EnableConfigurationProperties

@ConfigurationPropertiesScan

📝 代码示例

1. 配置文件 (application.yml)

app:
  server:
    timeout: 5000
    enabled: true

2. 配置类 POJO

import org.springframework.boot.context.properties.ConfigurationProperties;
// 声明前缀,Spring 会去查找 app.server.*
@ConfigurationProperties(prefix = "app.server")
public class ServerProperties {
    private Integer timeout;
    private boolean enabled;
    // 必须要有 Getter/Setter
    public Integer getTimeout() { return timeout; }
    public void setTimeout(Integer timeout) { this.timeout = timeout; }
    public boolean isEnabled() { return enabled; }
    public void setEnabled(boolean enabled) { this.enabled = enabled; }
}

3. 启用配置 (三种方式选其一)

2. 自动装配核心 (Auto Configuration)

@EnableAutoConfiguration

3. 条件注解 (Conditionals)

这一组注解决定了 Bean 是否会被创建并注册到容器中。这是实现“自动装配”逻辑判断的关键。

@Conditional

@ConditionalOnProperty

📝 代码示例:基于配置开关功能的 Bean

假设我们根据配置决定启用 "短信通知" 还是 "邮件通知"。

1. 配置文件

feature:
  notification: sms  # 改为 email 则加载 EmailService

2. 业务 Bean 定义

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class NotificationConfig {
    @Bean
    // 当 feature.notification = email 时生效
    @ConditionalOnProperty(prefix = "feature", name = "notification", havingValue = "email")
    public NotificationService emailService() {
        return new EmailNotificationService();
    }
    @Bean
    // 当 feature.notification = sms 时生效
    // matchIfMissing = true 表示如果不配这个配置,默认也启用 SMS
    @ConditionalOnProperty(prefix = "feature", name = "notification", havingValue = "sms", matchIfMissing = true)
    public NotificationService smsService() {
        return new SmsNotificationService();
    }
}

📝 代码示例:自定义 @Conditional (高阶用法)

如果 @ConditionalOnProperty 无法满足需求(比如判断操作系统、判断某个文件是否存在),可以使用 @Conditional

// 1. 定义判断逻辑
public class WindowsCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getProperty("os.name").contains("Windows");
    }
}
// 2. 使用注解
@Bean
@Conditional(WindowsCondition.class) // 只有在 Windows 系统下才加载该 Bean
public CmdService cmdService() {
    return new WindowsCmdService();
}

4. 总结速查表

注解作用域核心作用一句话记忆
@ConfigurationProperties定义配置属性与 POJO 的映射"把 yml 变成 Java 对象"
@EnableConfigurationProperties配置类激活并注册 @ConfigurationProperties 类"手动开关:启用属性对象"
@ConfigurationPropertiesScan启动类自动扫描并注册属性类"自动开关:扫码全注册"
@EnableAutoConfiguration启动类开启自动装配机制 (SPI)"Spring Boot 的自动魔法引擎"
@Conditional方法/类自定义复杂的加载条件"万能的 if 判断"
@ConditionalOnProperty方法/类基于配置属性值决定是否加载 Bean"根据开关 (Toggle) 加载 Bean"

到此这篇关于SpringBoot自动装配注解的实现示例的文章就介绍到这了,更多相关SpringBoot自动装配注解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文