文章目录详解测试思维来源原文:
详细解答
@Conditional是Spring4新提供的注释,起到按照一定条件进行判断,满足条件后在容器中注册bean的作用。
参数是Condition类的集合,让我们看看Condition接口
matches像正则类一样为true注入,应该不会注入false
在测试中创建person类,创建配置类,并返回linux、gates和类。
import lombok.allargsconstructor; import lombok.Data; @ data @ allargsconstructorpublicclassperson { privatestring name; 私有输入; }安排班记得打扫
importorg.spring framework.context.annotation.bean; importorg.spring framework.context.annotation.conditional; importorg.spring framework.context.annotation.configuration; @ configurationpublicclasspersonconfigure { @ bean (name=' bill ' ) conditional ) windows condition.class } publicperic } @bean(Linus ) ) conditional ) Linuxcondition.class ) ) public Person person2) ) returnnewperson ),48 )然后测试一波未平
import com.Alibaba.fast JSON.JSON; import com.QL.service test.condition demo.person; import org.junit.Test; importorg.spring framework.beans.beans exception; importorg.spring framework.context.application context; importorg.spring framework.context.applicationcontextaware; import java.util.Map; publicclassconditiontestextendsbasetestimplementsapplicationcontextaware { @ testpublicvoidtest1({ mapstring,personbeansansararatiction System.out.println ('当前系统为:' property ); system.out.println (JSON.tojsonstring ) beansoftype ); } publicapplicationcontextapplicationcontext; @ overridepublicvoidsetapplicationcontext (applicationcontext ) throwsbeansexception { this.application con throwsbeansexception (this.applicat )我直接继承baseTest测试类,然后实现ApplicationContextAware得到spring容器,简单方便。 如果不首先添加Conditional注释,则两者都有,创建两个类LinuxCondition、WindowsCondition来分别实现
importorg.spring framework.context.annotation.condition; importorg.spring framework.context.annotation.condition context; importorg.spring framework.core.env.environment; importorg.spring framework.core.type.annotatedtypemetadata; publicclasslinuxconditionimplementscondition { @ overridepublicbooleanmatches (conditioncontextconditioncontext, annotatedtypemetadataannotatedtypemetadata (环境环境=condition context.get environment ); string property=environment.getproperty (' OS.name ); 属性.内容(“Linux”) { return true; } return false; } importorg.spring framework.beans.factory.config.configurablelistablebeanfactory; importorg.spring framework.beans.factory.support.beandefinitionregistry; importorg.spring framework.context.annotation.condition; importorg.spring framework.context.annotation.condition context; importorg.spring framework.core.env.environment; importorg.spring framework.core.type.annotatedtypemetadata; 可用于publicclasswindowsconditionimplementscondition {/* * * @ paramconditioncontext :条件的上下文环境* @ paramannotatedtypemetadd @ overridepublicbooleanmatches (conditioncontextconditioncontext, annotatedtypemetadataannotatedtypemetadata (//IOC使用的beanfactoryconfigurablelistablebeanfactorybeanfactory=conditionction //当前环境信息environment environment=condition context.get environment (; bean定义的注册类beandefinitionregistryregistry=condition context.getregistry (; //当前系统名称string property=environment.getproperty (' OS.name );//如果包括windows,则为windows系统;true if (property.contains (' windows ' ) ) { return true; } return false; }这两个类实际上是获取并判断当前的运行环境,然后返回true、false。
添加每个@Conditional注释,然后执行:
发现只有gatesPerson类被注入,将执行环境变更为linux,试着进行了一次测试
结果:
@Conditional注释也可以加在注释里,加在注释里的效果也一样。
如果有多个条件呢? 总结都是回到true才注入.转载文章的结论。
如果我认为我的类返回true,方法返回false呢?
在我的类中恢复为false的话,在方法中恢复为true吗?
如果在类中返回false,我想它不会被注入,不看你的方法
如果类返回true,则确定方法。 请试试看
第一个在类中为True,
返回
第二个,在类别上为False
如果预想是正确的,
首先判断班级上,如果班级上不满意,就不直接加载
如果班级上满意,就判断方法上是否满意。
源代码https://github.com/stack Xu/study-auth configure
从原文:到:
3359 blog.csdn.net/xcy 1193068639/article/details/81491071