原创

注解-@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,否则就不注册

正文到此结束
本文目录