Spring组件注册之@Conditional-按条件注册bean

@Conditional通过不同的判断条件,来注册对应的bean
可以配置在类上:满足此条件下,这个类下面的bean都会注册
也可以配置在方法上:方法满足条件才会注册对应方法
下面写一个例子(以写在方法上为例子)
通过查看@Conditional注解源码,需要写对应的条件类来结合使用,此类需要实现Condition接口,重写其方法

public interface Condition {

	/**
	 * Determine if the condition matches.
	 * @param context the condition context
	 * @param metadata metadata of the {@link org.springframework.core.type.AnnotationMetadata class}
	 * or {@link org.springframework.core.type.MethodMetadata method} being checked.
	 * @return {@code true} if the condition matches and the component can be registered
	 * or {@code false} to veto registration.
	 */
	boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);

1.Condition 条件类如下
判断是linux系统的条件类

package chd.condition;

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 LinuxCondition implements Condition{
	// 是否为linux系统
	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;
	}
	
}

判断是windows系统的条件类

package chd.condition;

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 windowsCondition  implements Condition{
	
	/*
	 * ConditionContext:判断条件使用的上下文、
	 * AnnotatedTypeMetadata:注释信息
	 * */
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
		// 是否为windows系统
		//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");
		if(property.contains("Windows")) {
			return true;
		}
		return false;
	}
}	

2…写一个配置类

package config;

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

import chd.condition.LinuxCondition;
import chd.condition.windowsCondition;
import spring.Person;

@Configuration
public class MainConfig2 {
	/*@Conditional:按照一定的条件进行判断,满足条件给容器中注册bean
	 * */
	@Conditional({windowsCondition.class})//注册条件为windosw条件类的判断
	@Bean(value="bill")
	public Person person01() {
		return new Person("bill", "28");
	}
	@Conditional({LinuxCondition.class})//注册条件位linux
	@Bean(value="danney")
	public Person person02() {
		return new Person("danney", "38");
	}
}

3.写一个测试类测试

@Test
	public void test02() {
	String[] names=	applicationContext.getBeanNamesForType(Person.class);
		
		//动态获取环境变量的值:获取操作系统的名字
		ConfigurableEnvironment environment=applicationContext.getEnvironment();
		String property=environment.getProperty("os.name");
		System.out.println(property);
		
		for (String name1 : names) {
			System.out.println(name1);
		}
		//获取bean的id和bean的对象
	 Map <String,Person> persons=applicationContext.getBeansOfType(Person.class);
	 System.out.println(persons);
	}

结果系统位windows系统,只注册windowscondition类中所判断的
在这里插入图片描述
4.把系统环境切换成liunx系统试一下,这里只需要配置一些变量就可以,不需要换操作系统。
写入(-Dos.name=linux)
在这里插入图片描述
再次测试一下(结果就是linux条件类下所对应判断的结果)
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微微笑再加油

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值