Spring條件注解用法案例分析
本文實(shí)例講述了Spring條件注解用法。分享給大家供大家參考,具體如下:
一 點(diǎn)睛
Spring 4 提供了一個(gè)更通用的基于條件的Bean的創(chuàng)建,即使用@Conditional注解。
@Conditional根據(jù)滿足僅一個(gè)特定條件創(chuàng)建一個(gè)特定的Bean。也就是根據(jù)特定的條件來控制Bean的創(chuàng)建行為,這樣就可以利用這個(gè)特性進(jìn)行一些自動(dòng)的配置。
二 項(xiàng)目說明
以不同的操作系統(tǒng)為條件,通過實(shí)現(xiàn)@Condition接口,并重寫matches方法來構(gòu)造條件。若在windows系統(tǒng)下運(yùn)行,則輸出列表命令為dir;若在Linux操作系統(tǒng)下運(yùn)行程序,則輸出列表命令為ls。
三 實(shí)戰(zhàn)
1 判斷條件定義
1.1 windows的判定條件
package com.wisely.highlight_spring4.ch3.conditional;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class WindowsCondition implements Condition {
public boolean matches(ConditionContext context,
AnnotatedTypeMetadata metadata) {
return context.getEnvironment().getProperty("os.name").contains("Windows");
}
}
1.2 Linux的判定條件
package com.wisely.highlight_spring4.ch3.conditional;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class LinuxCondition implements Condition {
public boolean matches(ConditionContext context,
AnnotatedTypeMetadata metadata) {
return context.getEnvironment().getProperty("os.name").contains("Linux");
}
}
2 不同系統(tǒng)下的Bean類
2.1 接口
package com.wisely.highlight_spring4.ch3.conditional;
public interface ListService {
public String showListCmd();
}
2.2 Window下創(chuàng)建的Bean類
package com.wisely.highlight_spring4.ch3.conditional;
public class WindowsListService implements ListService {
@Override
public String showListCmd() {
return "dir";
}
}
2.3 Linux下所創(chuàng)建的Bean類
package com.wisely.highlight_spring4.ch3.conditional;
public class LinuxListService implements ListService{
@Override
public String showListCmd() {
return "ls";
}
}
3 配置類
package com.wisely.highlight_spring4.ch3.conditional;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConditionConifg {
@Bean
@Conditional(WindowsCondition.class) //符合window條件,則實(shí)例化WindowsListService
public ListService windowsListService() {
return new WindowsListService();
}
@Bean
@Conditional(LinuxCondition.class) //符合Linux條件,則實(shí)例化LinuxListService
public ListService linuxListService() {
return new LinuxListService();
}
}
4 主類
package com.wisely.highlight_spring4.ch3.conditional;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ConditionConifg.class);
ListService listService = context.getBean(ListService.class);
System.out.println(context.getEnvironment().getProperty("os.name")
+ "系統(tǒng)下的列表命令為: "
+ listService.showListCmd());
context.close();
}
}
四 運(yùn)行
windows下運(yùn)行結(jié)果如下:
Windows 10系統(tǒng)下的列表命令為: dir
五 擴(kuò)展
如果把LinuxCondition條件改成和WindowsCondition一樣的條件會(huì)怎樣呢?即有兩個(gè)條件都匹配會(huì)怎樣呢?
修改后的代碼如下:
public class LinuxCondition implements Condition {
public boolean matches(ConditionContext context,
AnnotatedTypeMetadata metadata) {
return context.getEnvironment().getProperty("os.name").contains("Windows");
}
}
修改后再運(yùn)行,報(bào)錯(cuò)了:
Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.wisely.highlight_spring4.ch3.conditional.ListService] is defined: expected single matching bean but found 2: linuxListService,windowsListService
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:365)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968)
at com.wisely.highlight_spring4.ch3.conditional.Main.main(Main.java:11)
報(bào)錯(cuò)信息很明顯:
[com.wisely.highlight_spring4.ch3.conditional.ListService] is defined: expected single matching bean but found 2: linuxListService,windowsListService
匹配到了兩條,所以針對條件判斷,設(shè)計(jì)程序時(shí),只能匹配上一條,否則會(huì)拋異常。
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
- Spring條件注解@Conditional示例詳解
- Spring Boot 自動(dòng)配置之條件注解淺析
- Spring的組合注解和元注解原理與用法詳解
- 詳解Java的Spring框架中的注解的用法
- Spring常用注解匯總
- Spring的注解配置與XML配置之間的比較
- 詳解Java Spring各種依賴注入注解的區(qū)別
- Spring @Bean vs @Service注解區(qū)別
- 詳解Spring Data JPA使用@Query注解(Using @Query)
- Spring 4.0新功能:@Conditional注解詳細(xì)介紹
- 淺談SpringBoot中的@Conditional注解的使用
- Spring注解@Conditional案例解析
相關(guān)文章
Java中的for循環(huán)結(jié)構(gòu)及實(shí)例
這篇文章主要介紹了Java中的for循環(huán)結(jié)構(gòu)及實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
java.lang.OutOfMemoryError 錯(cuò)誤整理及解決辦法
這篇文章主要介紹了java.lang.OutOfMemoryError 錯(cuò)誤整理及解決辦法的相關(guān)資料,需要的朋友可以參考下2016-10-10
springboot中通過jwt令牌校驗(yàn)及前端token請求頭進(jìn)行登錄攔截實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于springboot中如何通過jwt令牌校驗(yàn)及前端token請求頭進(jìn)行登錄攔截的相關(guān)資料,需要的朋友可以參考下2024-08-08

