spring-boot學習筆記之Conditional

zhuyuansj發表於2017-04-17

今天看了@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


相關文章