当前位置:首页 > 技术 > 正文内容

Spring条件装配注解详解

访客 技术 2026年6月25日 1

一、@Conditional注解基础

@Conditional是Spring Framework 4引入的条件注解,用于根据特定条件决定是否向容器注册Bean。该注解实现了运行时动态 Bean 装配的能力。

示例代码

定义一个服务接口:

package com.example.conditional;

/**
 * 文件列表服务接口
 */
public interface FileListService {
    String getListCommand();
}

Linux环境实现类:

package com.example.conditional;

/**
 * Linux环境下的文件列表服务实现
 */
public class LinuxFileListService implements FileListService {
    @Override
    public String getListCommand() {
        return "ls";
    }
}

Windows环境实现类:

package com.example.conditional;

/**
 * Windows环境下的文件列表服务实现
 */
public class WindowsFileListService implements FileListService {
    @Override
    public String getListCommand() {
        return "dir";
    }
}

Windows条件判断类:

package com.example.conditional;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * Windows操作系统条件判断
 */
public class WindowsOsCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        String osName = context.getEnvironment().getProperty("os.name");
        return osName != null && osName.contains("Windows");
    }
}

Linux条件判断类:

package com.example.conditional;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * Linux操作系统条件判断
 */
public class LinuxOsCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        String osName = context.getEnvironment().getProperty("os.name");
        return osName != null && osName.contains("Linux");
    }
}

配置类:

package com.example.conditional;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
 * Bean配置类
 */
@Configuration
public class ServiceConfiguration {

    @Bean
    @Conditional(WindowsOsCondition.class)
    public FileListService windowsFileListService() {
        return new WindowsFileListService();
    }

    @Bean
    @Conditional(LinuxOsCondition.class)
    public FileListService linuxFileListService() {
        return new LinuxFileListService();
    }
}

测试启动类:

package com.example.conditional;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 应用启动类
 */
public class ApplicationStartup {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(
                ServiceConfiguration.class);

        FileListService service = ctx.getBean(FileListService.class);
        String osName = ctx.getEnvironment().getProperty("os.name");
        System.out.println(osName + "系统下的列表命令为: " + service.getListCommand());
    }
}

运行结果:

Windows 10系统下的列表命令为: dir

二、@ConditionalOnClass注解

@ConditionalOnClass注解用于判断类路径上是否存在指定的类,只有当目标类存在时才创建对应的Bean。

例如:@ConditionalOnClass(JdbcTemplate.class) 表示只有当classpath中存在JdbcTemplate类时,才会实例化该Bean。这在自动配置场景中非常常见,用于实现可选依赖的功能。

三、@ConditionalOnBean注解

@ConditionalOnBean用于检查容器中是否已存在指定的Bean,仅当目标Bean存在时才创建当前Bean。

示例代码

实体类定义:

package com.example.model;

import lombok.Data;

/**
 * 父亲实体类
 */
@Data
public class Father {
    private String name;
    private int age;
    private Child child;
}
package com.example.model;

import lombok.Data;

/**
 * 孩子实体类
 */
@Data
public class Child {
    private String name;
    private int age;
}

配置类:

package com.example.conditional;

import com.example.model.Child;
import com.example.model.Father;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 条件配置类
 */
@Configuration
public class ConditionalConfiguration {

    // 注释掉此Bean后,Father的注入将失败
    /*@Bean
    public Child childBean() {
        Child child = new Child();
        child.setAge(8);
        child.setName("小华");
        return child;
    }*/

    @Bean
    @ConditionalOnBean(Child.class)
    public Father fatherBean(Child child) {
        Father father = new Father();
        father.setAge(38);
        father.setName("小王爸爸");
        father.setChild(child);
        return father;
    }
}

控制器测试类:

package com.example.conditional;

import com.example.model.Father;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 测试控制器
 */
@Controller
public class TestController {

    @Autowired(required = false)
    private Father father;

    @ResponseBody
    @GetMapping("/test")
    public void testConditionalOnBean() {
        System.out.println("Father bean: " + father);
    }
}

测试结果分析:

场景一:当容器中不存在Child Bean时,如果直接使用@Autowired注入Father,启动时会抛出异常:

Field father in com.example.conditional.TestController required a bean of type 'com.example.model.Father' that could not be found.

解决方案是在@Autowired注解中设置required=false,允许依赖为null,此时启动正常但father为null。

场景二:取消Child Bean的注释后重新启动,控制台输出:

Father(name=小王爸爸, age=38, child=Child(name=小华, age=8))

四、其他条件注解

  • @ConditionalOnClass:类路径上存在指定类时才创建Bean
  • @ConditionalOnExpression:Spring EL表达式结果为true时才创建Bean
  • @ConditionalOnMissingBean:容器中不存在指定Bean时才创建
  • @ConditionalOnMissingClass:类路径上不存在指定类时才创建
  • @ConditionalOnNotWebApplication:非Web应用时才创建Bean

五、@ConditionalOnProperty注解

@ConditionalOnProperty用于根据配置文件中的属性值来决定是否创建Bean。

语法示例:

@ConditionalOnProperty(prefix = "person", name = "super.enable", matchIfMissing = false, havingValue = "true")

参数说明:

  • prefix:配置属性的前缀
  • name:配置属性的名称(必填)
  • havingValue:与配置值对比,相同则条件成立
  • matchIfMissing:当配置属性不存在时的匹配行为,默认为false

示例:@ConditionalOnProperty(prefix = "aspectLog", name = "enable", havingValue = "true", matchIfMissing = true)

这表示当配置文件中存在aspectLog.enable=true时功能开启;如果配置文件未设置该属性,由于matchIfMissing=true,也会默认开启。

相关文章

Linux crontab 详解

1) crontab 是什么cron 是 Linux 的定时任务守护进程;crontab 是用来编辑/查看“按时间周期执行命令”的表(cron table)。常见两类:用户 crontab:每个用户一份(crontab -e 编辑)系统级 crontab / cron.d:可指定执行用户(/etc/crontab、/etc/cron.d/*)2) crontab 时间...

富文本里可以允许的 HTML 属性

一、所有标签默认允许的安全属性(极少)class        (可选)id           (通常建议禁用)title️ 注意:id 容易被滥用做锚点注入,很多系统直接禁用class 允许的话最好只允许固定前缀(如 editor-*)二、a 标签允许属性<a href="" t...

Mac 安装 Node.js 指南

方法一:通过官网安装包(最简单,适合初学者)如果你只是想快速安装并开始使用,这是最直接的方法。访问 Node.js 官网。页面会显示两个版本:LTS (Recommended For Most Users):长期支持版,最稳定。建议选这个。Current:最新特性版,包含最新功能但可能不够稳定。下载 .pkg 安装包并运行。按照安装向导点击“下一步”即可完成。方法二:使用 Homebrew 安装(...

Dom\HTML_NO_DEFAULT_NS 的副作用:自动加闭合标签

在使用Dom\HTMLDocument时,Dom\HTML_NO_DEFAULT_NS 将禁止在解析过程中设置元素的命名空间, 此设置是为了与DOMDocument向后兼容而存在的。当使用它时,已知的一个副作用就是:自动加闭合标签例如 </img> 为什么会这样?当你使用:Dom\HTML_NO_DEFAULT_NS文档会变成 无命名空间模式,此时内部更接近 XML...

Laravel 事件和监听器创建

在 Laravel 中,使用 Artisan 命令创建 Events(事件) 和 Listeners(监听器) 是非常高效的。你可以通过以下几种方式来实现:1. 手动创建单个 Event如果你只想创建一个事件类,可以使用 make:event 命令:Bashphp artisan make:event UserRegistered执行后,文件将生成在 app/Even...

自定义域名解析神器 dnsmasq

什么是 dnsmasq?dnsmasq 是一个轻量级、功能强大的网络服务工具,专为小型和中等规模网络设计。它是一个综合的网络基础设施解决方案[1]。dnsmasq 能做什么?功能说明应用场景DNS 转发与缓存将 DNS 查询转发到上游服务器(ISP、Google DNS 等),并在本地缓存结果加快 DNS 查询速度,减少外部 DNS 流量本地 DNS解析本地网络设备的主机名,无需编辑&n...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。