今天看了@Conditional,自己根據(jù)以下文章練了下,根據(jù)自己的理解操作的
轉(zhuǎn)載出處:http://wiselyman.iteye.com/blog/2213054
17.1 @Conditional
@Conditional為按照條件配置spring的bean提供了支持,即滿足某種條件下,怎么配置對(duì)應(yīng)的bean;
應(yīng)用場(chǎng)景
當(dāng)某一個(gè)jar包在classpath中的時(shí)候,配置某幾個(gè)bean;
當(dāng)某一個(gè)bean配置好后,會(huì)自動(dòng)配置一個(gè)特定的bean;
當(dāng)某種環(huán)境變量被設(shè)置后,創(chuàng)建某個(gè)bean;
@Conditional為敏捷開(kāi)發(fā)所提倡的原則"習(xí)慣優(yōu)于配置"提供了支持;
@Conditional是Spring Boot快速開(kāi)發(fā)框架實(shí)現(xiàn)"習(xí)慣優(yōu)于配置"的核心技術(shù);
演示在windows和linux系統(tǒng)下,初始化不同的bean,windows和linux作為判斷條件;
條件的構(gòu)造需要類實(shí)現(xiàn)Condition接口,并實(shí)現(xiàn)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");? ? }}
接口
packagecom.wisely.conditional;publicinterfaceCommandService{publicStringshowListCommand();}
WindowsCommnadService
packagecom.wisely.conditional;publicclassWindowsCommnadServiceimplementsCommandService{publicStringshowListCommand() {return"dir";? ? }}
LinuxCommandService
packagecom.wisely.conditional;publicclassLinuxCommandServiceimplementsCommandService{publicStringshowListCommand() {return"ls";? ? }}
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();? ? }}
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();? ? }}
輸出結(jié)果
dir
Linux下(本例沒(méi)有切換到linux,直接修改os.name為L(zhǎng)inux)
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();? ? }}
輸出結(jié)果
ls