Spring示例講解條件注入方法
簡介
說明
本文用實例介紹Spring的條件注入的用法。
@Component、@Configuration+@Bean都可以與條件注入的注解結合。
@Component+條件注解
Bean
package com.example.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Component; @Component @ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true") public class MyComponent { public MyComponent() { System.out.println("[MyComponent#MyComponent]"); } }
application.yml
custom:
myComponent:
enabled: true
運行結果:
[MyComponent#MyComponent]
若將application.yml的custom.myComponent.enabled去掉,或者設置為非true值,則不會輸出上邊的運行結果。
@Configuration+@Bean+條件注解
Bean
package com.example.config; public class MyComponent { public MyComponent() { System.out.println("[MyComponent#MyComponent]"); } }
配置類
package com.example.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyConfig { @Bean @ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true") public MyComponent getMyComponent() { return new MyComponent(); } }
application.yml
custom:
myComponent:
enabled: true
運行結果:
[MyComponent#MyComponent]
若將application.yml的custom.myComponent.enabled去掉,或者設置為非true值,則不會輸出上邊的運行結果。
@Configuration+條件注解+@Bean
Bean
package com.example.config; public class MyComponent { public MyComponent() { System.out.println("[MyComponent#MyComponent]"); } }
配置類
package com.example.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true") public class MyConfig { @Bean public MyComponent getMyComponent() { return new MyComponent(); } }
application.yml
custom:
myComponent:
enabled: true
運行結果:
[MyComponent#MyComponent]
若將application.yml的custom.myComponent.enabled去掉,或者設置為非true值,則不會輸出上邊的運行結果。
自定義Condition
自定義的condition的matches方法返回值為true時,才會創(chuàng)建bean。
條件類
//判斷當前系統(tǒng)是否是Mac
import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; public class MyCondition implements Condition { @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { return conditionContext.getEnvironment().getProperty("os.name").contains("Mac"); } }
@Configuration public class Config { @Conditional(MyCondition.class) @Bean public String condition() { System.err.println("This is mac"); return ""; } }
到此這篇關于Spring示例講解條件注入方法的文章就介紹到這了,更多相關Spring條件注入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java高級之HashMap中的entrySet()方法使用
這篇文章主要介紹了Java高級之HashMap中的entrySet()方法使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03SpringBoot整合MongoDB的實現(xiàn)代碼
自己本科時候一直使用的是Mysql,目前的課題組使用的是MongoDB,因此就花了一部分時間整理了一下,實現(xiàn)springboot與MongoDB的整合,并且實現(xiàn)基本的增刪改查操作,從頭到尾給出一個完整的案例。2021-05-05