spring boot中的那些conditional
spring boot中为我们提供了丰富的conditional来让我们得以非常方便的在项目中向容器中添加bean。本文主要是对各个注解进行解释并辅以代码说明其用途。
所有conditionalonxxx的注解都可以放置在class或是method上,如果方式在class上,则会决定该class中所有的@bean注解方法是否执行。 @conditional
下面其他的conditional注解均是语法糖,可以通过下面的方法自定义conditionalonxxx
conditional注解定义如下,接收实现condition接口的class数组。
public @interface conditional {
class<? extends condition>[] value();
}
而condition接口只有一个matchs方法,返回是否匹配的结果。
public interface condition {
boolean matches(conditioncontext context, annotatedtypemetadata metadata);
}
//linuxcondition,为方便起见,去掉判断代码,直接返回true了
public class linuxcondition implements condition {
@override
public boolean matches(conditioncontext conditioncontext, annotatedtypemetadata annotatedtypemetadata) {
return true;
}
}
//windowscondition,为方便起见,去掉判断代码,直接返回false了
public class windowscondition implements condition {
@override
public boolean matches(conditioncontext conditioncontext, annotatedtypemetadata metadata) {
return false;
}
}
@data
@tostring
@allargsconstructor
@noargsconstructor
public class person {
private string name;
private integer age;
}
//配置类
@configuration
public class beanconfig {
@bean(name = "bill")
@conditional({windowscondition.class})
public person person1(){
return new person("bill gates",62);
}
@bean("linus")
@conditional({linuxcondition.class})
public person person2(){
return new person("linus",48);
}
}
public class apptest {
annotationconfigapplicationcontext applicationcontext = new annotationconfigapplicationcontext(beanconfig.class);
@test
public void test(){
string osname = applicationcontext.getenvironment().getproperty("os.name");
system.out.println("当前系统为:" + osname);
map<string, person> map = applicationcontext.getbeansoftype(person.class);
system.out.println(map);
}
}
@data
@allargsconstructor
@tostring
public class computer {
private string name;
}
@configuration
public class beanconfig {
@bean(name = "notebookpc")
public computer computer1(){
return new computer("笔记本电脑");
}
@conditionalonmissingbean(computer.class)
@bean("reservepc")
public computer computer2(){
return new computer("备用电脑");
}
}
public class testapp {
annotationconfigapplicationcontext applicationcontext = new annotationconfigapplicationcontext(beanconfig.class);
@test
public void test1(){
map<string,computer> map = applicationcontext.getbeansoftype(computer.class);
system.out.println(map);
}
}
public interface fighter {
void fight();
}
public class fightera implements fighter {
@override
public void fight() {
system.out.println("使用光明套装");
}
}
public class fighterb implements fighter {
@override
public void fight() {
system.out.println("使用暗黑套装");
}
}
van是武士,使用套装进行战斗
@data
@allargsconstructor
@noargsconstructor
public class van {
private fighter fighter;
public void fight(){
fighter.fight();
}
}
vanconfiga/b实例化武士
@configuration
@conditionalonclass({fightera.class})
public class vanconfiga {
@primary
@bean
public van vana(){
return new van(new fightera());
}
}
@configuration
@conditionalonclass({fighterb.class})
public class vanconfigb {
@bean
public van vanb(){
return new van(new fighterb());
}
}
@data
public class testbean {
private string name;
}
@configuration
@conditionalonexpression("#{${test.enabled:true} }")
//@conditionalonexpression("'zz'.equalsignorecase('${test.name2}')")
//@conditionalonexpression("new integer('${test.account}')==1")
public class beanconfig {
@bean
public testbean testbean(){
return new testbean("我是美猴王");
}
}
@springbootapplication
public class testappcommand implements commandlinerunner {
@autowired
private testbean testbean;
public static void main(string[] args) {
springapplication.run(testappcommand.class, args);
}
@override
public void run(string... args) throws exception {
system.out.println(testbean.getname());
}
}
@springbootapplication
public class testappcommand implements commandlinerunner {
@autowired
private testbean testbean;
public static void main(string[] args) {
springapplication.run(testappcommand.class, args);
}
@override
public void run(string... args) throws exception {
system.out.println(testbean.getname());
}
}
@conditionalonjava
可以通过java的版本进行判断。
@data
public class testbean {
}
@configuration
@conditionalonjava(javaversion.eight)
public class beanconfig {
@bean
public testbean testbean(){
return new testbean();
}
}
public class testapp {
annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(beanconfig.class);
@test
public void test(){
map<string,testbean> map = context.getbeansoftype(testbean.class);
system.out.println(map);
}
}
@configuration
@conditionalonresource(resources = "classpath:application.yml")
public class beanconfig {
@bean
public testbean testbean(){
return new testbean();
}
}
public class testapp {
annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(beanconfig.class);
@test
public void test(){
map<string,testbean> map = context.getbeansoftype(testbean.class);
system.out.println(map);
}
}
@data
@allargsconstructor
@noargsconstructor
public class beana {
private string name;
}
@configuration
public class beanaconfig {
@bean
@primary
public beana bean1(){
return new beana("bean1");
}
@bean(autowirecandidate = false)
public beana bean2(){
return new beana("bean2");
}
//@bean(autowirecandidate = false)
public beana bean3(){
return new beana("bean3");
}
}
@data
public class beanb {
}
@configuration
@autoconfigureafter(beanaconfig.class)
@conditionalonsinglecandidate(beana.class)
public class beanbconfig {
@bean
public beanb targetbean(){
return new beanb();
}
}
public class testapp {
annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(beanaconfig.class, beanbconfig.class);
@test
public void test(){
map<string,beana> map = context.getbeansoftype(beana.class);
system.out.println(map);
map<string,beanb> map2 = context.getbeansoftype(beanb.class);
system.out.println(map2);
}
}