命名Spring Bean

•Bean 的名稱每個Bean 擁有一個或多個標識符(identifiers),這些標識符在Bean 所在的容器必須是唯一的,不是所在的應用。通常,一個Bean 僅有一個標識符,如果需要額外的,可考慮使用別名(Alias)來擴充。

在基於XML 的配置元信息中,開發人員可用id 或者name 屬性來規定Bean 的標識符。通常Bean 的標識符由字母組成,允許出現特殊字符。如果要想引入Bean 的別名的話,可在name 屬性使用半角逗號(“,”)或分號(“;”) 來間隔。

Bean 的id 或name 屬性並非必須制定,如果留空的話,容器會爲Bean 自動生成一個唯一的名稱。Bean 的命名儘管沒有限制,不過官方建議採用駝峯的方式,更符合Java 的命名約定。

Bean 名稱生成器(BeanNameGenerator)

•由Spring Framework 2.0.3 引入,框架內建兩種實現:

DefaultBeanNameGenerator:默認通用BeanNameGenerator 實現

•AnnotationBeanNameGenerator:基於註解掃描的BeanNameGenerator 實現,起始於

Spring Framework 2.5,關聯的官方文檔:

With component scanning in the classpath, Spring generates bean names for unnamed components, following the rules described earlier: essentially, taking the simple class name and turning its initial character to lower-case. However, in the (unusual) special case when there is more than one character and both the first and second characters are upper case, the original casing gets preserved. These are the same rules as defined by java.beans.Introspector.decapitalize (which Spring uses here)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章