Spring框架注解式开发(IDEA)——关于Conditional注解的具体使用(超详细)

本文详细介绍了如何在Spring Boot应用中使用@Conditional注解,通过操作系统类型动态决定Person Bean的注入。通过Linux和Windows条件类,展示了如何在不同环境下控制Bean的加载,有助于实现灵活的环境依赖注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@Conditional介绍

它的作用是按照一定的条件进行判断,需要注入的Bean满足给定条件才可以注入到IOC容器中。

举个经典例子:操作系统中你是用window还是linux

如果是window系统就不能用linux,如果是linux就不能用windows

下面开始先做使用配置

1.1 创建domain类

package com.Bean;

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Bean{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

1.2 创建配置类Config,定义Bean

package com.Config;

import com.Bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;

public class MainConfig {


    @Bean("bill")
    public Person person1(){
        return new Person("Bill",60);
    }

    @Bean("Lin")
    public Person person2(){
        return new Person("Lin",48);
    }
}

1.3 创建测试类,运行

@Test
public void Test03(){
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
    String[] beanNamesForType = annotationConfigApplicationContext.getBeanNamesForType(Person.class);
    ConfigurableEnvironment environment = annotationConfigApplicationContext.getEnvironment();
    String property = environment.getProperty("os.name");
    System.out.println(property);
    for (String name:beanNamesForType) {
        System.out.println(name);
    }

}

 运行后我们发现IOC容器中管理了三个Bean,且操作系统现在为windows 10,下面我们来使用Conditional注解操作

二 Conditional注解使用详情

2.1创建二个类,windows,linux

package com.Conditional;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class Linux implements Condition {
    //ConditionContext:判断上下文环境
    //AnnotatedTypeMetadata,注释信息
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        //获取IOC使用beanfactory
        ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
        //获取当前环境信息
        Environment environment = conditionContext.getEnvironment();
        String property = environment.getProperty("os.name");
        //如果是linux系统返回真
        if(property.contains("linux")){
            return true;
        }
        return false;
    }
}
package com.Conditional;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class windows implements Condition {
    //ConditionContext:判断上下文环境
    //AnnotatedTypeMetadata,注释信息
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        //获取当前环境信息
        Environment environment = conditionContext.getEnvironment();
        String property = environment.getProperty("os.name");
        //如果是Windows系统返回真
        if(property.contains("Windows")){
            return true;
        }
        return false;
    }
}

2.2 在Config中添加Conditional注解

@Conditional({windows.class})
@Bean("bill")
public Person person1(){
    return new Person("Bill",60);
}

@Conditional({Linux.class})
@Bean("Lin")
public Person person2(){
    return new Person("Lin",48);
}

2.3运行后

 最终我们得到了答案,因为Lin是在Linux操作系统下才会运行的,但是我们计算机是windows系统所以不会运行,则不会加载到IOC容器中

三 Conditional注解可以配置在方法上,也可以配置在类上

注意:如果我们配置到类上就必须满足这个操作条件才会让类方法的Bean加载到IOC容器中

3.1在类上添加注解Conditional 并设置为Linux系统

package com.Config;

import com.Bean.Person;
import com.Conditional.Linux;
import com.Conditional.windows;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;

@Conditional({Linux.class})
public class MainConfig {

   @Conditional({windows.class})
    @Bean("bill")
    public Person person1(){
        return new Person("Bill",60);
    }

    @Conditional({Linux.class})
    @Bean("Lin")
    public Person person2(){
        return new Person("Lin",48);
    }
}

3.2运行

因为我们操作系统是Windows 10,如果不是Liunx系统,下面的方法全部都不会加载到IOC容器中,所以为空

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值