Spring Boot實戰筆記(八)-- Spring高級話題(條件註解@Conditional)
一、條件註解@Conditional
在之前的學習中,通過活動的profile,我們可以獲得不同的Bean。Spring4提供了一個更通用的基於條件的Bean的創建,即使用@Conditional註解。
@Conditional根據滿足某一個特定條件創建一個特定的Bean。比方說,當某一個jar包在一個類路徑下的時候,自動配置一個或多個Bean;或者只有某個Bean被創建才會創建另外一個Bean。總的來說,就是根據特定條件來控制Bean的創建行為,這樣我們可以利用這個特性進行一些自動的配置。
下面示例將以不同的操作系統為條件,我們通過實現Condition接口,並重寫其matches方法來構造判斷條件。若在Windows系統下運行程序,則輸出列表命令為dir;若在Linux系統下運行程序,則輸出列表命令為ls。
示例:
1.判斷條件定義
(1)判定Windows的條件
package com.ecworking.condition; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; public class WindowsCondition implements Condition { @Overridepublic boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { return conditionContext.getEnvironment().getProperty("os.name").contains("Windows"); } }
(2)判定Linux的條件
package com.ecworking.condition; import org.springframework.context.annotation.Condition;import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; public class LinuxCondition implements Condition { @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { return conditionContext.getEnvironment().getProperty("os.name").contains("Linux"); } }
(3)判定Mac的條件
package com.ecworking.condition; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; public class MacCondition implements Condition{ @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { return conditionContext.getEnvironment().getProperty("os.name").contains("Mac"); } }
2.不同系統下的Bean類
(1)接口
package com.ecworking.condition; public interface ListService { String showListCmd(); }
(2)Windows下所要創建的Bean類
package com.ecworking.condition; public class WindowsListService implements ListService{ @Override public String showListCmd() { return "dir"; } }
(3)Linux下所要創建的Bean類
package com.ecworking.condition; public class LinuxListService implements ListService{ @Override public String showListCmd() { return "ls"; } }
(4)Mac下所要創建的Bean類
package com.ecworking.condition; public class MacListService implements ListService{ @Override public String showListCmd() { return "ls"; } }
3.配置類
package com.ecworking.condition; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; @Configuration public class ConditionConfig { @Bean @Conditional(WindowsCondition.class) // 通過@Conditional註解,符合Windows條件則實例化WindowsListService public ListService windowsListService(){ return new WindowsListService(); } @Bean @Conditional(LinuxCondition.class) // 通過@Conditional註解,符合Linux條件則實例化LinuxListService public ListService linuxListService(){ return new LinuxListService(); } @Bean @Conditional(MacCondition.class) // 通過@Conditional註解,符合Mac條件則實例化MacListService public ListService macListService(){ return new MacListService(); } }
4.運行
package com.ecworking.condition; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConditionConfig.class); ListService listService = context.getBean(ListService.class); System.out.println(context.getEnvironment().getProperty("os.name") + "系統下的列表命令為:" + listService.showListCmd()); } }
運行結果:
在首次運行的時候只使用了書中Windows和Linux的實例,結果運行 ListService listService = context.getBean(ListService.class); 時報錯,因為沒有符合條件的實例創建,原因是我使用的是Mac,以前一直以為Mac就是Linux系統,結果發現Mac的操作系統是Mac OS,所以加上MacCondition和MacListService後正常運行。
Spring Boot實戰筆記(八)-- Spring高級話題(條件註解@Conditional)