spring boot中的條件裝配bean的實(shí)現(xiàn)
條件裝配
從Spring Framework 3.1開始,允許在Bean裝配時(shí)增加前置條件判斷。
啥是條件裝配
在bean裝配前的條件判斷。比如@Profile(是在spring3.1中引入),@Contditional(spring4.0中引入)
實(shí)現(xiàn)方式:注解方式,編程方式。
假設(shè)我們現(xiàn)在有一個(gè)多數(shù)據(jù)求和計(jì)算的小需求,定義兩種方式Java7和Java8,然后使用條件裝配的方式分別裝配不同的bean。
首先我們定義一個(gè)接口
public interface CalculateService {
/**
* 從多個(gè)整數(shù) sum 求和
* @param values 多個(gè)整數(shù)
* @return sum 累加值
*/
Integer sum(Integer... values);
}
其次是兩種不同的實(shí)現(xiàn)方式,Java7的方式
@Profile("Java7")
@Service
public class Java7CalculateService implements CalculateService {
@Override
public Integer sum(Integer... values) {
System.out.println("Java 7 for 循環(huán)實(shí)現(xiàn) ");
int sum = 0;
for (int i = 0; i < values.length; i++) {
sum += values[i];
}
return sum;
}
}
Java8的實(shí)現(xiàn)
@Profile("Java8")
@Service
public class Java8CalculateService implements CalculateService {
@Override
public Integer sum(Integer... values) {
System.out.println("Java 8 Lambda 實(shí)現(xiàn)");
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));
}
}
我們?cè)谶@里使用了@Profile("Java8")注解來(lái)表明對(duì)應(yīng)的profile。然后我定義一個(gè)啟動(dòng)類在里面配置裝配哪一個(gè)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, 2, 3, 4, 5, 6, 7, 8, 9, 10));
// 關(guān)閉上下文
context.close();
}
}
使用基于@ConditionalOnSystemProperty注解的方式實(shí)現(xiàn)。
首先我們定義一個(gè)注解,這里定義了一個(gè)屬性和一個(gè)值。
/**
* Java 系統(tǒng)屬性 條件判斷
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnSystemPropertyCondition.class)
public @interface ConditionalOnSystemProperty {
/**
* Java 系統(tǒng)屬性名稱
* @return
*/
String name();
/**
* Java 系統(tǒng)屬性值
* @return
*/
String value();
}
定義一個(gè)條件判斷的類,當(dāng)這個(gè)類中的條件滿足是才會(huì)裝載bean,這個(gè)實(shí)現(xiàn)類實(shí)現(xiàn)org.springframework.context.annotation.Condition,AnnotatedTypeMetadata可以獲取的到注解中的name和value信息,假設(shè)我們現(xiàn)在實(shí)現(xiàn)判斷系統(tǒng)屬性和注解中的配置的一樣就加載bean,System.getProperty("user.name")獲取當(dāng)前系統(tǒng)下的用戶名,我的mac創(chuàng)建的用戶名叫yanghongxing,如果我們?cè)谧⒔庵信渲玫膙alue是yanghongxing則裝載這個(gè)bean。
/**
* 系統(tǒng)屬性條件判斷
*
*/
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);
}
}
最后在啟動(dòng)類中啟動(dòng)
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);
// 通過(guò)名稱和類型獲取 helloWorld Bean
String helloWorld = context.getBean("helloWorld", String.class);
System.out.println("helloWorld Bean : " + helloWorld);
// 關(guān)閉上下文
context.close();
}
}
我們可以在OnSystemPropertyCondition實(shí)現(xiàn)復(fù)雜的裝載類的條件,判斷是否裝載某個(gè)bean。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- spring如何使用xml裝配bean
- 基于XML配置Spring的自動(dòng)裝配過(guò)程解析
- Spring裝配Bean教程之XML安裝配置bean詳解
- sersync2 完全安裝配置說(shuō)明(二) 可選功能與xml高級(jí)配置
- Spring注解實(shí)現(xiàn)Bean自動(dòng)裝配示例詳解
- Spring IOC裝配Bean過(guò)程解析
- Spring自動(dòng)裝配Bean實(shí)現(xiàn)過(guò)程詳解
- spring裝配bean的3種方式總結(jié)
- 在Spring中自動(dòng)裝配Bean的屬性
- spring在IoC容器中裝配Bean詳解
- 詳解SpringBean基于XML的裝配
相關(guān)文章
kill命令在Java應(yīng)用中使用的注意事項(xiàng)小結(jié)
這篇文章主要給大家介紹了關(guān)于kill命令在Java應(yīng)用中使用的注意事項(xiàng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
淺談Java并發(fā)中ReentrantLock鎖應(yīng)該怎么用
本文主要介紹了ava并發(fā)中ReentrantLock鎖的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
SpringBoot實(shí)現(xiàn)釘釘機(jī)器人消息推送的示例代碼
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)釘釘機(jī)器人消息推送的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Spring Security Remember me使用及原理詳解
這篇文章主要介紹了Spring Security Remember me使用及原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
詳解如何實(shí)現(xiàn)SpringBoot的底層注解
今天給大家?guī)?lái)的文章是如何實(shí)現(xiàn)SpringBoot的底層注解,文中有非常詳細(xì)的介紹及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴很有幫助,需要的朋友可以參考下2021-06-06
Java中 this和super的用法與區(qū)別小結(jié)
在Java的學(xué)習(xí)與開發(fā)者我們經(jīng)常遇到this和super關(guān)鍵字,本文主要介紹了Java中 this和super的用法與區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12

