本文主要是介绍注解-@Conditional的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
@Conditional注解是条件注册IOC的注解
满足条件的将被注册进IOC容器里面,不满足的将不被注册
@Conditional注解传入的参数是一个org.springframework.context.annotation.Condition类的数组
org.springframework.context.annotation.Condition类是一个接口类,需要自己去实现它
我这里的配置类里面注册了3个Person的Bean,一个是普通的Person,一个是Bill Gates的(Windows公司的老板),还有个是Linus(Linux)
IOC可以获取Environment,environment可以获取“os.name”,通过该方法判断当前操作系统是Windows还是Linux,然后注册对应的Bean
示例代码:
package com.debuggg.test1.main3;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;@Configuration
public class MainConfig2 {/*** 作者 ZYL* 功能描述 : @Conditional注解* 条件注册注解,满足条件的就注册,不满足的就不注册到ioc里面* Conditional里面是@org.springframework.context.annotation.Condition类的实现* 里面有个matches方法,两个参数:* org.springframework.context.annotation.ConditionContext:判断条件能使用的上下文(环境* org.springframework.core.type.AnnotatedTypeMetadata:注释信息* 日期 2020-04-12 21:17* 参数* 返回值 com.debuggg.test1.main3.Person*/@Beanpublic Person person(){return new Person("张三",18);}@Conditional({WindowsConditional.class})@Beanpublic Person person01(){return new Person("Bill Gates",62);}@Conditional({LinuxConditional.class})@Beanpublic Person person02(){return new Person("Linus",47);}
}
WindowsConditional
package com.debuggg.test1.main3;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 WindowsConditional implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {//TODO 是否是Linux系统//1.能获取到ioc使用的beanFactoryConfigurableListableBeanFactory 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");//判断操作系统是包含了Windows就返回trueif(property.contains("Windows")){return true;}return false;}
}
LinuxConditional
package com.debuggg.test1.main3;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 LinuxConditional implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {Environment environment = context.getEnvironment();String property = environment.getProperty("os.name");if(property.contains("Linux")){return true;}return false;}
}
@Conditional还可以放在类上面,表示如果该类满足条件,就注册该类里面的所有Bean,否则就不注册
这篇关于注解-@Conditional的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!