原文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,装配失败