Spring条件注解@Conditoinal+ Profile环境切换应用@Profile

Spring条件注解

一、创建一个maven项目

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.1.5.RELEASE</version>
    </dependency>
</dependencies>

二、接口以及实现类

public interface Food {
    String showName();
}
public class Rice implements Food {
    public String showName() {
        return"米饭";
    }
}
public class Noodles implements Food {
    public String showName() {
        return"面条";
    }
}

三、配置类

两个 Bean 的名字都为 food,这不是巧合,而是有意取的。两个 Bean 的返回值都为其父类对象 Food
每个 Bean 上都多了 @Conditional 注解,当 @Conditional 注解中配置的条件类的 matches 方法返回值为 true 时,对应的 Bean 就会生效。

@Configuration
public class JavaConfig{

	@Bean("food")
	@Conditoinal(RiceConditionl.class)
	Food rice(){
		return new Rice();
	}

	@Bean("food")
	@Conditional(NoodlesCondition.class)
	Food noddles(){
		return new Noddles();
	}
}

四、条件Condition接口的实现类,用来做出判断

在 matches 方法中做条件属性判断,当系统属性中的 people 属性值为 ‘北方人’ 的时候,NoodlesCondition 的条件得到满足,当系统中 people 属性值为 ‘南方人’ 的时候,RiceCondition 的条件得到满足,换句话说,哪个条件得到满足,一会就会创建哪个 Bean

public class NoodlesCondition implements Condition{
	
	@Override
	public boolean matches(ConditionContext context,AnnotatedTypeMetadata metadata){
		
		return context.getEnvironment().getProperty("people").equals("北方人");
	}
}


public class RiceCondition implements Condition {
	
	@Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getProperty("people").equals("南方人");
    }
}

六、测试

public class Main {
    public static void main(String[] args) {
	
		// AnnotationConfigApplicationContext 实例用来加载 Java 配置类
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        
        //添加一个 property 到 environment 中,添加完成后,再去注册我们的配置类
        ctx.getEnvironment().getSystemProperties().put("people", "南方人");
        ctx.register(JavaConfig.class);
		
		//刷新容器
        ctx.refresh();

		//从容器中去获取 food 的实例了,这个实例会根据 people 属性的不同,而创建出来不同的 Food 实例
        Food food = (Food) ctx.getBean("food");
        System.out.println(food.showName());
    }
}

Profile环境切换

更改配置类
这次不需要条件注解了,取而代之的是 @Profile

@Configuration
public class JavaConfig{
	
	@Bean("food")
	@Profile("南方人")
	Food rice(){
		return new Rice();
	}

	@Bean("food")
	@Profile("北方人")
	Food noddles(){
		return new Noddles();
	}
}

测试

public class Main {
    public static void main(String[] args) {
    
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.getEnvironment().setActiveProfiles("南方人");
        
        ctx.register(JavaConfig.class);
        ctx.refresh();
        
        Food food = (Food) ctx.getBean("food");
        System.out.println(food.showName());
    }
}

@Profile注解定义

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(ProfileCondition.class) //条件类
public @interface Profile {
	String[] value();
}
class ProfileCondition implements Condition {

	@Override
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
		MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
		if (attrs != null) {
			for (Object value : attrs.get("value")) {
				if (context.getEnvironment().acceptsProfiles(Profiles.of((String[]) value))) {
					returntrue;
				}
			}
			returnfalse;
		}
		returntrue;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值