Spring注解驱动开发实战 | 第五篇:组件注册-@Conditional按照条件注册bean

按照一定的条件进行判断,满足条件给容器中注册bean

创建com.wsc.condition包,在包内创建WindowsCondition.java和LinuxCondition.java

package com.wsc.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;

//判断是否linux系统
public class LinuxCondition implements Condition {

	/**
	 * ConditionContext:判断条件能使用的上下文(环境)
	 * AnnotatedTypeMetadata:注释信息
	 */
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
		// TODO是否linux系统
		//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");
		
		//可以判断容器中的bean注册情况,也可以给容器中注册bean
		boolean definition = registry.containsBeanDefinition("person");
		if(property.contains("linux")){
			return true;
		}
		
		return false;
	}
}

 

package com.wsc.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;

//判断是否windows系统
public class WindowsCondition implements Condition {


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

}

 

在MainConfigIOC2.java中添加@Condition注解,person01和person02

package com.wsc.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 com.wsc.bean.Person;
import com.wsc.condition.LinuxCondition;
import com.wsc.condition.WindowsCondition;

@Configuration
public class MainConfigIOC2 {
	@Scope
	@Lazy
	@Bean("person")
	public Person person(){
		System.out.println("把Person添加到容器....");
		return new Person("赤脚医生", 18);
	}
	
	/**
	 * @Conditional({Condition}) : 
	 * 按照一定的条件进行判断,满足条件给容器中注册bean
	 * 
	 * 如果系统是windows,给容器中注册("bill")
	 * 如果是linux系统,给容器中注册("linus")
	 */
	@Conditional(WindowsCondition.class)
	@Bean("bill")
	public Person person01(){
		return new Person("Bill Gates",65);
	}
	
	@Conditional(LinuxCondition.class)
	@Bean("linus")
	public Person person02(){
		return new Person("linus", 58);
	}
	
}

在IOCTest.java中添加testCondition测试方法

 /*@Condition
		 * 
		 * 按条件加载bean
	     */
	    @Test
		public void testCondition(){
	    	AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigIOC2.class);
			String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
			ConfigurableEnvironment environment = applicationContext.getEnvironment();
			//动态获取环境变量的值;Windows 7
			String property = environment.getProperty("os.name");
			System.out.println(property);
			for (String name : namesForType) {
				System.out.println(name);
			}
			
			Map persons = applicationContext.getBeansOfType(Person.class);
			System.out.println(persons);
			
		}

1 在windows环境下

运行testCondition测试方法,结果如下:

 

2 在Linux环境下(模拟)

模拟linux环境更改

Spring注解驱动开发实战 | 第五篇:组件注册-@Conditional按照条件注册bean_第1张图片

运行testCondition测试方法,结果如下:

--------------------------------

源码下载

你可能感兴趣的:(spring注解,Spring注解驱动开发实战)