Spring示例講解條件注入方法
簡(jiǎn)介
說明
本文用實(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]");
}
}配置類
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]");
}
}配置類
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。
條件類
//判斷當(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)文章
Spring Boot中單例類實(shí)現(xiàn)對(duì)象的注入方式
這篇文章主要介紹了Spring Boot中單例類實(shí)現(xiàn)對(duì)象的注入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
JavaEE中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
這篇文章主要為大家詳細(xì)介紹了JavaEE中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例,感興趣的小伙伴們可以參考一下2016-05-05
Java結(jié)合EasyExcel構(gòu)建復(fù)雜多級(jí)表頭
在Java開發(fā)中,處理Excel文件時(shí),構(gòu)建復(fù)雜的多級(jí)表頭是一項(xiàng)常見且具有挑戰(zhàn)性的任務(wù),下面小編就來和大家聊聊如何通過自定義方法實(shí)現(xiàn)多級(jí)表頭的構(gòu)建吧2025-03-03

