Spring @Conditional注解——转载学习 2020/09/12

原文https://blog.csdn.net/xcy1193068639/article/details/81491071

前言:

@Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件给容器注册bean。

@Conditional的定义:

//此注解可以标注在类和方法上
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME) 
@Documented
public @interface Conditional {
    Class<? extends Condition>[] value();
}

需要使用这个注解,要传入一个Class数组

//源码
package org.springframework.context.annotation;

import org.springframework.core.type.AnnotatedTypeMetadata;

@FunctionalInterface
public interface Condition {
    boolean matches(ConditionContext var1, AnnotatedTypeMetadata var2);
}

所以重写类来实现这个接口,在使用@Conditional时传入不同的实现类

例@Conditional({A.class,B.class})

示例:

首先,创建Person类:

package com.example.zhujie;

public class Person {
    private String name;
    private String pwd;

    public Person() {
    }

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

    public String getName() {
        return name;
    }

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

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

在不同的操作系统环境下,我需要在项目启动时注入的Person对象属性不同,所以创建两个类实现Condition接口,

举例中,windows的matches方法返回true,则装配其对应的Bean

package com.example.zhujie;

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

public class Windows implements Condition {

    @Override
    public boolean matches(ConditionContext var1, AnnotatedTypeMetadata var2) {
        //方法返回true,则注入该注解修饰的@Bean
        return true;
    }
}
package com.example.zhujie;

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

public class Linux implements Condition {
    @Override
    public boolean matches(ConditionContext var1, AnnotatedTypeMetadata var2) {
        //方法返回true,则注入该注解修饰的@Bean
        return false;
    }
}

 

创建PersonConfig类,用于配置两个Person实例并注入,根据情况不同,注入实例不同

package com.example.zhujie;

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

@Configuration
public class PersonConfig {

    @Conditional(Windows.class)
    @Bean("张三")
    public Person getPerson1(){
        return new Person("张三","123");
    }

    @Bean("李四")
    @Conditional(Linux.class)
    public Person getPerson2(){
        return new Person("李四","456");
    }
}

测试类

package com.example.zhujie;

import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Map;

public class ConfigrationTest {

    private AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);

    @Test
    public void test1(){
        Map<String, Person> beansOfType = applicationContext.getBeansOfType(Person.class);
        System.out.println(beansOfType);
    }
}

装配情况

{张三=Person{name='张三', pwd='123'}}

@Conditional(Windows.class)中windows的实现方法返回true,装配成功
@Conditional(Linux.class)中windows的实现方法返回false,装配失败
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值