Spring - 组件注册(二)

Spring - 组件注册(二)

Scroll Down

组件注册

@Conditional - 按照条件注册

源码:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {
	/**
	 * All {@link Condition Conditions} that must {@linkplain Condition#matches match}
	 * in order for the component to be registered.
	 */
	Class<? extends Condition>[] value();
}

我们需要传入一个 Class<? extends Condition> 数组,且每个自定义的 condition 都需要实现 Condition 接口。
我们在这里先创建两个Condition,分别判断是Windows系统还是Linux系统

WindowsCondition

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;

/**
 * @author : lwj
 * createTime  : 2020/7/2 17:21
 * description : Reprint please indicate the source
 * 判断是否是Windows系统
 */
public class WindowsCondition implements Condition {

    /**
     *
     * @param context 判断条件能使用的上下文(环境
     * @param metadata 注释信息
     * @return true or false
     */
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

        // 获取到IOC使用的beanfactory
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        // 获取类加载器
        ClassLoader classLoader = context.getClassLoader();
        // 获取当前环境信息
        Environment environment = context.getEnvironment();
        // 获取到bean定义的注册类
        BeanDefinitionRegistry registry = context.getRegistry();

        String property = environment.getProperty("os.name");
        assert property != null;
        return property.contains("Windows");
    }
}

LinuxCondition

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;

/**
 * @author : lwj
 * createTime  : 2020/7/2 17:20
 * description : Reprint please indicate the source
 * 判断是否是Linux系统
 */
public class LinuxCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Environment environment = context.getEnvironment();
        // 获取到bean定义的注册类
        BeanDefinitionRegistry registry = context.getRegistry();

        String property = environment.getProperty("os.name");
        assert property != null;
        return property.contains("Linux");
    }
}

在注册的bean上面加入@Condition注解

package cn.lwjppz.config;

import cn.lwjppz.condition.LinuxCondition;
import cn.lwjppz.condition.WindowsCondition;
import cn.lwjppz.entites.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
 * @author : lwj
 * createTime  : 2020/7/2 17:31
 * description : Reprint please indicate the source
 */
@Configuration
public class MainConfig3 {
    /**
     * @Conditional({Condition}) 按照一定的条件进行判断,满足条件给容器中注册bean
     * @return
     *
     * 如果是 Windows 系统 则给容器注册 bill
     * 如果是 Linux 系统,则注册 jack
     */
    @Bean("bill")
    @Conditional(WindowsCondition.class)
    public User user01 () {
        return new User(2, "Bill", 62);
    }

    @Bean("jack")
    @Conditional(LinuxCondition.class)
    public User user02 () {
        return new User(3, "jack", 22);
    }
}

测试

image.png

由于系统是Windows的,所以只给容器中加入了bill。

@Import - 给容器中导入组件

新建一个Color类

public class Color {
}

加入@Import注解

import cn.lwjppz.condition.LinuxCondition;
import cn.lwjppz.condition.WindowsCondition;
import cn.lwjppz.entites.Color;
import cn.lwjppz.entites.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @author : lwj
 * createTime  : 2020/7/2 17:31
 * description : Reprint please indicate the source
 */
@Configuration
// @Import 导入组件,id默认为组件的全类名
@Import(Color.class)
public class MainConfig3 {
    /**
     * @Conditional({Condition}) 按照一定的条件进行判断,满足条件给容器中注册bean
     * @return
     *
     * 如果是 Windows 系统 则给容器注册 bill
     * 如果是 Linux 系统,则注册 jack
     */
    @Bean("bill")
    @Conditional(WindowsCondition.class)
    public User user01 () {
        return new User(2, "Bill", 62);
    }

    @Bean("jack")
    @Conditional(LinuxCondition.class)
    public User user02 () {
        return new User(3, "jack", 22);
    }
}

测试

可见Color已被加入容器中

支付宝 微信