之前看到这篇博客,写ConditionalOnClass的用法,自己实践了一下,感觉有点问题,原文如下:
https://blog.csdn.net/lucyTheSlayer/article/details/80430912
于是自己更正了一下,代码结构如下:
贴上完整的代码:
程序入口:ConditionalDemoApplication:
@SpringBootApplication public class ConditionalDemoApplication implements CommandLineRunner { @Autowired private Van van; public static void main(String[] args) { SpringApplication.run(ConditionalDemoApplication.class, args); } @Override public void run(String... args) throws Exception{ van.fight(); } }
Van.java
@Service public class Van { @Autowired private Fighter fighter; public void fight(){ System.out.println("van:boy next door,do you like 玩游戏"); fighter.fight(); } }
Fighter.java及其实现类:
public interface Fighter { void fight(); }
@Service
public class Babana implements Fighter {
@Override
public void fight(){
System.out.println("Banana: *的气息,蕉迟但到");
}
}
@Service
public class Billy implements Fighter {
public void fight(){
System.out.println("Billy:吾乃新日暮里的王,三界哲学的主宰。");
}
}
VanConfig:
@Configuration
public class VanConfig {
@Bean
@ConditionalOnBean(Billy.class)
public Fighter fighter(){
return new Billy();
}
@Bean
@ConditionalOnMissingBean
public Fighter fighter2(){
return new Babana();
}
}
1、运行程序,输入结果如下:
2、如果将Billy Bean的代码注释掉:
@Configuration
public class VanConfig {
/*@Bean
@ConditionalOnBean(Billy.class)
public Fighter fighter(){
return new Billy();
}*/
@Bean
@ConditionalOnMissingBean
public Fighter fighter2(){
return new Babana();
}
}
重新运行,输入结果如下:
3、或者,我们将Billy上的@Service注解注释掉,让springboot扫描不到该类:
//@Service public class Billy implements Fighter { public void fight(){ System.out.println("Billy:吾乃新日暮里的王,三界哲学的主宰。"); } }
同时恢复VanConfig里的配置:
@Configuration public class VanConfig { @Bean @ConditionalOnBean(Billy.class) public Fighter fighter(){ return new Billy(); } @Bean @ConditionalOnMissingBean public Fighter fighter2(){ return new Babana(); } }
再次运行,输入结果如下(与第2次试验的效果相同):
4、在第3个试验的基础上,我们再做一个试验,把@ConditionalOnBean(Billy.class)注释掉:
@Configuration public class VanConfig { @Bean // @ConditionalOnBean(Billy.class) public Fighter fighter(){ return new Billy(); } @Bean @ConditionalOnMissingBean public Fighter fighter2(){ return new Babana(); } }
重新运行,结果如下:
这说明,在不添加ConditionalOnBean条件时,Billy的注入已不再受任何约束的
结论:
1、@ConditionalOnBean(xxx.class)就是为了判断 xxx.class是否存在,并已注释了springboot容器里了;
2、@ConditionalOnMissingBean 则是在第一点不存在的情况下起作用;
当然,我们还继续作一些测试,比如,去掉Babana上的@Service等,再看一看效果会怎样。
好了,分享完毕,该回去干活了...