spring boot中的條件裝配bean的實現
阿新 • • 發佈:2020-01-07
條件裝配
從Spring Framework 3.1開始,允許在Bean裝配時增加前置條件判斷。
啥是條件裝配
在bean裝配前的條件判斷。比如@Profile(是在spring3.1中引入),@Contditional(spring4.0中引入)
實現方式:註解方式,程式設計方式。
假設我們現在有一個多資料求和計算的小需求,定義兩種方式Java7和Java8,然後使用條件裝配的方式分別裝配不同的bean。
首先我們定義一個介面
public interface CalculateService { /** * 從多個整數 sum 求和 * @param values 多個整數 * @return sum 累加值 */ Integer sum(Integer... values); }
其次是兩種不同的實現方式,Java7的方式
@Profile("Java7") @Service public class Java7CalculateService implements CalculateService { @Override public Integer sum(Integer... values) { System.out.println("Java 7 for 迴圈實現 "); int sum = 0; for (int i = 0; i < values.length; i++) { sum += values[i]; } return sum; } }
Java8的實現
@Profile("Java8") @Service public class Java8CalculateService implements CalculateService { @Override public Integer sum(Integer... values) { System.out.println("Java 8 Lambda 實現"); int sum = Stream.of(values).reduce(0,Integer::sum); return sum; } public static void main(String[] args) { CalculateService calculateService = new Java8CalculateService(); System.out.println(calculateService.sum(1,2,3,4,5,6,7,8,9,10)); } }
我們在這裡使用了@Profile("Java8")註解來表明對應的profile。然後我定義一個啟動類在裡面配置裝配哪一個bean
@SpringBootApplication(scanBasePackages = "com.service") public class CalculateServiceBootstrap { public static void main(String[] args) { ConfigurableApplicationContext context = new SpringApplicationBuilder(CalculateServiceBootstrap.class) .web(WebApplicationType.NONE) .profiles("Java8") .run(args); // CalculateService Bean 是否存在 CalculateService calculateService = context.getBean(CalculateService.class); System.out.println("calculateService.sum(1...10) : " + calculateService.sum(1,10)); // 關閉上下文 context.close(); } }
使用基於@ConditionalOnSystemProperty註解的方式實現。
首先我們定義一個註解,這裡定義了一個屬性和一個值。
/** * Java 系統屬性 條件判斷 * */ @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE,ElementType.METHOD }) @Documented @Conditional(OnSystemPropertyCondition.class) public @interface ConditionalOnSystemProperty { /** * Java 系統屬性名稱 * @return */ String name(); /** * Java 系統屬性值 * @return */ String value(); }
定義一個條件判斷的類,當這個類中的條件滿足是才會裝載bean,這個實現類實現org.springframework.context.annotation.Condition,AnnotatedTypeMetadata可以獲取的到註解中的name和value資訊,假設我們現在實現判斷系統屬性和註解中的配置的一樣就載入bean,System.getProperty("user.name")獲取當前系統下的使用者名稱,我的mac建立的使用者名稱叫yanghongxing,如果我們在註解中配置的value是yanghongxing則裝載這個bean。
/** * 系統屬性條件判斷 * */ public class OnSystemPropertyCondition implements Condition { @Override public boolean matches(ConditionContext context,AnnotatedTypeMetadata metadata) { Map<String,Object> attributes = metadata.getAnnotationAttributes(ConditionalOnSystemProperty.class.getName()); String propertyName = String.valueOf(attributes.get("name")); String propertyValue = String.valueOf(attributes.get("value")); String javaPropertyValue = System.getProperty(propertyName); return propertyValue.equals(javaPropertyValue); } }
最後在啟動類中啟動
public class ConditionalOnSystemPropertyBootstrap { @Bean @ConditionalOnSystemProperty(name = "user.name",value = "yanghongxing") public String helloWorld() { return "Hello,World Honson"; } public static void main(String[] args) { ConfigurableApplicationContext context = new SpringApplicationBuilder(ConditionalOnSystemPropertyBootstrap.class) .web(WebApplicationType.NONE) .run(args); // 通過名稱和型別獲取 helloWorld Bean String helloWorld = context.getBean("helloWorld",String.class); System.out.println("helloWorld Bean : " + helloWorld); // 關閉上下文 context.close(); } }
我們可以在OnSystemPropertyCondition實現複雜的裝載類的條件,判斷是否裝載某個bean。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。