Spring示例講解條件注入方法
簡(jiǎn)介
說(shuō)明
本文用實(shí)例介紹Spring的條件注入的用法。
@Component、@Configuration+@Bean都可以與條件注入的注解結(jié)合。
@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
運(yùn)行結(jié)果:
[MyComponent#MyComponent]
若將application.yml的custom.myComponent.enabled去掉,或者設(shè)置為非true值,則不會(huì)輸出上邊的運(yùn)行結(jié)果。
@Configuration+@Bean+條件注解
Bean
package com.example.config; public class MyComponent { public MyComponent() { System.out.println("[MyComponent#MyComponent]"); } }
配置類(lèi)
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
運(yùn)行結(jié)果:
[MyComponent#MyComponent]
若將application.yml的custom.myComponent.enabled去掉,或者設(shè)置為非true值,則不會(huì)輸出上邊的運(yùn)行結(jié)果。
@Configuration+條件注解+@Bean
Bean
package com.example.config; public class MyComponent { public MyComponent() { System.out.println("[MyComponent#MyComponent]"); } }
配置類(lèi)
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
運(yùn)行結(jié)果:
[MyComponent#MyComponent]
若將application.yml的custom.myComponent.enabled去掉,或者設(shè)置為非true值,則不會(huì)輸出上邊的運(yùn)行結(jié)果。
自定義Condition
自定義的condition的matches方法返回值為true時(shí),才會(huì)創(chuàng)建bean。
條件類(lèi)
//判斷當(dāng)前系統(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 ""; } }
到此這篇關(guān)于Spring示例講解條件注入方法的文章就介紹到這了,更多相關(guān)Spring條件注入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java線性結(jié)構(gòu)中棧、隊(duì)列和串的基本概念和特點(diǎn)詳解
前幾天小編給大家介紹了Java線性結(jié)構(gòu)中的鏈表,除了鏈表這種結(jié)構(gòu)之外,實(shí)際上還有棧、隊(duì)列、串等結(jié)構(gòu),那么這些結(jié)構(gòu)又有哪些特點(diǎn)呢,本文就給大家詳細(xì)的介紹一下,感興趣的小伙伴跟著小編一起來(lái)看看吧2023-07-07Java中一個(gè)for語(yǔ)句導(dǎo)致無(wú)窮大死循環(huán)的例子
這篇文章主要介紹了Java中一個(gè)for語(yǔ)句導(dǎo)致無(wú)窮大死循環(huán)的例子,本文給出的是一個(gè)很特別的例子,這個(gè)例子會(huì)跟你所想的結(jié)果不一樣,需要的朋友可以參考下2015-06-06java連接MySQL數(shù)據(jù)庫(kù)的代碼
這篇文章主要為大家詳細(xì)介紹了java連接MySQL數(shù)據(jù)庫(kù)的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Java高級(jí)之HashMap中的entrySet()方法使用
這篇文章主要介紹了Java高級(jí)之HashMap中的entrySet()方法使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03SpringBoot整合MongoDB的實(shí)現(xiàn)代碼
自己本科時(shí)候一直使用的是Mysql,目前的課題組使用的是MongoDB,因此就花了一部分時(shí)間整理了一下,實(shí)現(xiàn)springboot與MongoDB的整合,并且實(shí)現(xiàn)基本的增刪改查操作,從頭到尾給出一個(gè)完整的案例。2021-05-05