注解-@Conditional的使用

@Conditional注解是条件注册IOC的注解

满足条件的将被注册进IOC容器里面,不满足的将不被注册

@Conditional注解传入的参数是一个org.springframework.context.annotation.Condition类的数组

org.springframework.context.annotation.Condition类是一个接口类,需要自己去实现它
我这里的配置类里面注册了3个Person的Bean,一个是普通的Person,一个是Bill Gates的(Windows公司的老板),还有个是Linus(Linux)
IOC可以获取Environment,environment可以获取“os.name”,通过该方法判断当前操作系统是Windows还是Linux,然后注册对应的Bean
示例代码:

package com.debuggg.test1.main3;

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

@Configuration
public class MainConfig2 {

    /**
     * 作者 ZYL
     * 功能描述 : @Conditional注解
     * 条件注册注解,满足条件的就注册,不满足的就不注册到ioc里面
     * Conditional里面是@org.springframework.context.annotation.Condition类的实现
     * 里面有个matches方法,两个参数:
     *  org.springframework.context.annotation.ConditionContext:判断条件能使用的上下文(环境
     *  org.springframework.core.type.AnnotatedTypeMetadata:注释信息
     * 日期 2020-04-12 21:17
     * 参数
     * 返回值 com.debuggg.test1.main3.Person
     */
    @Bean
    public Person person(){
        return new Person("张三",18);
    }

    @Conditional({WindowsConditional.class})
    @Bean
    public Person person01(){
        return new Person("Bill Gates",62);
    }

    @Conditional({LinuxConditional.class})
    @Bean
    public Person person02(){
        return new Person("Linus",47);
    }
}

WindowsConditional

package com.debuggg.test1.main3;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
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 WindowsConditional implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //TODO 是否是Linux系统
        //1.能获取到ioc使用的beanFactory
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        //2.获取类加载器
        ClassLoader classLoader = context.getClassLoader();
        //3.获取当前环境信息
        Environment environment = context.getEnvironment();
        //4.获取到bean定义的注册类
        BeanDefinitionRegistry registry = context.getRegistry();
        //通过环境信息获取到当前的操作系统版本
        String property = environment.getProperty("os.name");

        //判断操作系统是包含了Windows就返回true
        if(property.contains("Windows")){
            return true;
        }
        return false;
    }
}

LinuxConditional

package com.debuggg.test1.main3;

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 LinuxConditional implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Environment environment = context.getEnvironment();
        String property = environment.getProperty("os.name");
        if(property.contains("Linux")){
            return true;
        }
        return false;
    }
}

@Conditional还可以放在类上面,表示如果该类满足条件,就注册该类里面的所有Bean,否则就不注册

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值