今天看了@Conditional,自己根据以下文章练了下,根据自己的理解操作的
转载出处:http://wiselyman.iteye.com/blog/2213054
17.1 @Conditional
@Conditional为按照条件配置spring的bean提供了支持,即满足某种条件下,怎么配置对应的bean;
应用场景
当某一个jar包在classpath中的时候,配置某几个bean;
当某一个bean配置好后,会自动配置一个特定的bean;
当某种环境变量被设置后,创建某个bean;
@Conditional为敏捷开发所提倡的原则"习惯优于配置"提供了支持;
@Conditional是Spring Boot快速开发框架实现"习惯优于配置"的核心技术;
17.2 示例
演示在windows和linux系统下,初始化不同的bean,windows和linux作为判断条件;
17.2.1 构造判断条件
条件的构造需要类实现Condition接口,并实现matches方法
WindowsCondition
packagecom.wisely.conditional;importorg.springframework.context.annotation.Condition;importorg.springframework.context.annotation.ConditionContext;importorg.springframework.core.type.AnnotatedTypeMetadata;publicclassWindowsConditionimplementsCondition{publicbooleanmatches(ConditionContextcontext,AnnotatedTypeMetadatametadata) {returncontext.getEnvironment().getProperty("os.name").contains("Windows"); }}
LinuxCondition
packagecom.wisely.conditional;importorg.springframework.context.annotation.Condition;importorg.springframework.context.annotation.ConditionContext;importorg.springframework.core.type.AnnotatedTypeMetadata;publicclassLinuxConditionimplementsCondition{publicbooleanmatches(ConditionContextcontext,AnnotatedTypeMetadatametadata) {returncontext.getEnvironment().getProperty("os.name").contains("Linux"); }}
17.2.2 编写不同条件下需要的bean
接口
packagecom.wisely.conditional;publicinterfaceCommandService{publicStringshowListCommand();}
WindowsCommnadService
packagecom.wisely.conditional;publicclassWindowsCommnadServiceimplementsCommandService{publicStringshowListCommand() {return"dir"; }}
LinuxCommandService
packagecom.wisely.conditional;publicclassLinuxCommandServiceimplementsCommandService{publicStringshowListCommand() {return"ls"; }}
17.2.3 编写配置类
packagecom.wisely.conditional;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Conditional;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassDemoConfig{@Bean@Conditional(WindowsCondition.class)publicCommandServicecommandService() {returnnewWindowsCommnadService(); }@Bean@Conditional(LinuxCondition.class)publicCommandServicelinuxEmailerService() {returnnewLinuxCommandService(); }}
17.2.4 测试
windows下
packagecom.wisely.conditional;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;publicclassMain{publicstaticvoidmain(String[]args) {AnnotationConfigApplicationContextcontext=newAnnotationConfigApplicationContext("com.wisely.conditional");CommandServicecs=context.getBean(CommandService.class);System.out.println(cs.showListCommand()); context.close(); }}
输出结果
dir
Linux下(本例没有切换到linux,直接修改os.name为Linux)
packagecom.wisely.conditional;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;publicclassMain{publicstaticvoidmain(String[]args) {System.setProperty("os.name","Linux");AnnotationConfigApplicationContextcontext=newAnnotationConfigApplicationContext("com.wisely.conditional");CommandServicecs=context.getBean(CommandService.class);System.out.println(cs.showListCommand()); context.close(); }}
输出结果
ls