国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

詳解Spring的autowire-candidate設(shè)計

瀏覽:16日期:2023-07-07 17:46:35
目錄Xml配置文件中的default-autowire-candidates屬性匹配邏輯算法PatternMatchUtils.simpleMatch總結(jié)Xml配置文件中的default-autowire-candidates屬性

有的同學(xué)對這個配置可能不熟悉或者說都不知道這個配置的存在,那首先我們看下default-autowire-candidates這個配置是放在何處的:

<beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:context='http://www.springframework.org/schema/context' xmlns:aop='http://www.springframework.org/schema/aop' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd default-autowire-candidates='service*'> <bean autowire-candidate='false'/> <bean /> <bean autowire='byType' /></beans>

在idea中我們可以點開 default-autowire-candidates這個屬性所在的spring-beans.xsd就能看到官方對這個屬性的注釋:

A default bean name pattern for identifying autowire candidates: e.g. 'Service', 'data', 'Service', 'dataService'. Also accepts a comma-separated list of patterns: e.g. 'Service,*Dao'. See the documentation for the ’autowire-candidate’ attribute of the ’bean’ element for the semantic details of autowire candidate beans.

簡單翻譯下也就是說這個屬性可以標(biāo)示配置文件中的所有Bean默認(rèn)能否成為自動注入候選者的名稱匹配模式,比如 'Service', 'data', 'Service', 'dataService'.也支持以逗號分隔的字符串模式列表:'Service,Dao'. 比如上面配置文件中配置的service就匹配了serviceA,serviceB兩個Bean.但是Spring的設(shè)計規(guī)定serviceA自身配置的autowire-candidate為false會覆蓋default-autowire-candidates配置,所以serviceA是不會成為自動注入的候選者。

匹配邏輯算法

我們深入到源碼中看下Spring是如何根據(jù)這個匹配模式來與自身bean名稱來匹配的

String autowireCandidate = ele.getAttribute(AUTOWIRE_CANDIDATE_ATTRIBUTE);if (''.equals(autowireCandidate) || DEFAULT_VALUE.equals(autowireCandidate)) { String candidatePattern = this.defaults.getAutowireCandidates(); if (candidatePattern != null) { String[] patterns = StringUtils.commaDelimitedListToStringArray(candidatePattern); bd.setAutowireCandidate(PatternMatchUtils.simpleMatch(patterns, beanName)); }}else { bd.setAutowireCandidate(TRUE_VALUE.equals(autowireCandidate));}

很清楚,在bean本身配置autowire-candidate為空或者默認(rèn)的情況下,Spring會把default-autowire-candidates字符串轉(zhuǎn)換成數(shù)組,然后依賴PatternMatchUtils類的simpleMatch方法來驗證當(dāng)前bean的名稱是否匹配,成功與否都會賦值給當(dāng)前bean的autowireCandidate屬性。其實最主要的還是PatternMatchUtils.simpleMatch方法

PatternMatchUtils.simpleMatch

public static boolean simpleMatch(@Nullable String pattern, @Nullable String str) { //pattern 匹配模式為空 或者待匹配字符串為空就返回false if (pattern == null || str == null) { return false; } //找到第一個* 在匹配模式字符串中的的索引 int firstIndex = pattern.indexOf(’*’); if (firstIndex == -1) { //索引為空的情況下就代表 模式字符串要和待匹配字符串相等。 return pattern.equals(str); } //*在第一位 if (firstIndex == 0) { //*在第一位 且匹配模式字符串長度為1 那就直接返回true ,比如 * if (pattern.length() == 1) { return true; } //找到下一個*的起始位置 int nextIndex = pattern.indexOf(’*’, firstIndex + 1); if (nextIndex == -1) { //如果沒有*了,就判斷 待匹配的字符串是否是以pattern結(jié)尾的。 //比如*service Aservice就滿足這種情況 return str.endsWith(pattern.substring(1)); } //截取第一個* 和之后一個* 之間的字符串 String part = pattern.substring(1, nextIndex); if (part.isEmpty()) { return simpleMatch(pattern.substring(nextIndex), str); } //str 是指待匹配的字符 int partIndex = str.indexOf(part); while (partIndex != -1) { if (simpleMatch(pattern.substring(nextIndex), str.substring(partIndex + part.length()))) { return true; } //從partIndex+1 開始計算part的索引 partIndex = str.indexOf(part, partIndex + 1); } return false; } //待匹配字符串的長度比 第一個*的索引 大或者相等的情況下 //截取模式字符串 0 到 第一個*號之間的字符串 ,截取 待匹配字符串 0 到 第一個*號之間的字符串 對比 //如果相等 ,再截取 模式字符串 第一個*號之后的字符串 和 待匹配 字符串 第一個*號之后的字符串 去做匹配 return (str.length() >= firstIndex && pattern.substring(0, firstIndex).equals(str.substring(0, firstIndex)) && simpleMatch(pattern.substring(firstIndex), str.substring(firstIndex)));}

這個Utils類的工具函數(shù)實現(xiàn)的字符串模糊匹配算法在我們?nèi)粘i_發(fā)中對字符串的操作方面也會有或多或少的幫助。

總結(jié)

Spring中的很多設(shè)計細(xì)節(jié)總是給我們很多驚喜,從中我們也可以很多小技巧,給我們?nèi)粘i_發(fā)會帶來不少啟發(fā)。

以上就是詳解Spring的autowire-candidate設(shè)計的詳細(xì)內(nèi)容,更多關(guān)于Spring的autowire-candidate設(shè)計的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 三级视频在线播放 | 色偷偷在线刺激免费视频 | 亚洲综合一区二区不卡 | 国产男女免费完整视频 | 欧美操操操操 | 黄色美女一级片 | 日韩一级一片 | 我要看a级毛片 | 中文日韩字幕一区在线观看 | 午夜性激福利免费观看 | 欧美视频三级 | 一级做a爰 | 欧美一级α片毛片免费观看 | 欧美一a级做爰 | 亚洲一区精品在线 | 亚洲欧美久久一区二区 | 在线视频久 | 精品在线观看一区 | 韩国三级大全久久网站 | 久久91精品国产一区二区 | 国产日产欧美a级毛片 | 日韩中文字幕精品一区在线 | 国产成人综合95精品视频免费 | 亚洲美女色成人综合 | 涩里番资源网站在线观看 | 亚洲在线久久 | 久久网在线 | 午夜成年人网站 | 加勒比综合网 | 黄色欧美视频 | 国产精品久久久久久久久99热 | 网站免费满18成年在线观看 | 亚洲一区二区中文 | 欧美另类视频一区二区三区 | 欧美成人性色区 | 精品无码久久久久久国产 | 色老头老太做爰视频在线观看 | 成年美女黄网站色大 | 精品韩国主播福利视频在线观看一 | 国产手机免费视频 | 在线观看国产一区二三区 |