5-组件注册-@Conditional按条件注册bean

本文知识点来源于尚硅谷,感谢尚硅谷为广大学子提供的优质教育资源,感谢各位老师热情指导,本文仅作为学习笔记使用,记录学习心得,如有不适,请联系作者。

@Conditional

按照一定条件进行判断,满足条件给容器中注册Bean
可作用在配置类上进行统一配置,也作用在方法上单独配置

示例:如果系统是windows系统,给容器注册girl,如果系统是linux系统,给容器注册boy

配置类不加@Conditional注解

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

import com.tedu.bean.Person;
/**
 * 配置类==配置文件xml
 * @author Administrator
 *
 */
@Configuration //告诉Spring这是一个配置类
public class MainConfig2 {
	
	@Bean//默认单实例Bean
	public Person person() {
		return new Person("李四",18,"男");
	}
	
	@Bean("girl")
	public Person person01() {
		return new Person("韩梅梅",18,"女");
	}
	
	@Bean("boy")
	public Person person02() {
		return new Person("小明",20,"男");
	}

测试类:

import java.util.Map;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.Environment;

import com.tedu.bean.Person;
import com.tedu.config.MainConfig2;

public class MainTest_IOC {

	@Test
	public void test_Conditional() {
		ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
		String[] beanNames = applicationContext.getBeanNamesForType(Person.class);
		//获取IOC容器所在环境变量
		Environment environment = applicationContext.getEnvironment();
		//获取当前操作系统
		String name = environment.getProperty("os.name");
		System.out.println("操作系统为:" + name);
		
		for (String beanName : beanNames) {
			System.out.println("beanName:" + beanName);
		}
		
		Map<String, Person> map = applicationContext.getBeansOfType(Person.class);
		System.out.println(map);
	}

输出:

操作系统为:Windows 7
beanName:person
beanName:girl
beanName:boy
{person=Person [name=李四, age=18, sex=], girl=Person [name=韩梅梅, age=18, sex=], boy=Person [name=小明, age=20, sex=]}

接下来测试@Conditional
新增条件类实现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;

/**
 * 判断是否为Windows系统
 *
 */
public class WindowsCondition implements Condition{
	
	/**
	 * context 判断条件能使用的上下文(环境)
	 * metadata 当前标注了@Conditional注解的注释信息
	 */
	@Override
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
		//1.能获取到ioc使用的beanFactory
		ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
		//2.获取类加载器
		ClassLoader classLoader = context.getClassLoader();
		//3.获取当前环境信息
		Environment environment = context.getEnvironment();
		//4.获取bean注册的定义类
		BeanDefinitionRegistry registry = context.getRegistry();
		//获取当前操作系统
		String name = environment.getProperty("os.name").toUpperCase();
		if (name.contains("WINDOWS")) {
			return true;
		}
		return false;
	}
public class LinuxCondition implements Condition{

	@Override
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
		Environment environment = context.getEnvironment();
		String name = environment.getProperty("os.name").toUpperCase();
		if (name.contains("LINUX")) {
			return true;
		}
		return false;
	}

}

我们当前为window系统,启动测试类输出:

操作系统为:Windows 7
beanName:person
beanName:girl
{person=Person [name=李四, age=18, sex=], girl=Person [name=韩梅梅, age=18, sex=]}

输出结果符合预期

我们修改启动配置将系统设为Linux
右键->Run As->Run Configurations…->Arguments
vm arguments中输入-Dos.name=linux,应用确定。
在这里插入图片描述
再次执行测试类,输出:

操作系统为:linux
beanName:person
beanName:boy
{person=Person [name=李四, age=18, sex=], boy=Person [name=小明, age=20, sex=]}

输出符合预期

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值