十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
本篇內容主要講解“如何實現(xiàn)Spring中生成Bean時默認生成名稱”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何實現(xiàn)Spring中生成Bean時默認生成名稱”吧!
網(wǎng)站建設哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、重慶小程序開發(fā)、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了荔波免費建站歡迎大家使用!
定義一個類如下:
@Componentpublic class MXTable{ ...... }復制代碼
通過ApplicationContext.getBean("mXTable")
獲取這個Bean對象,但是為NULL,導致調用的時候出現(xiàn)空指針異常。
在使用注解生成Bean的時候,如果沒有指定Bean的名稱,如@Componet("mytable")
,則Spring會使用默認的名稱生成策略,具體源碼如下:
public class AnnotationBeanNameGenerator implements BeanNameGenerator { private static final String COMPONENT_ANNOTATION_CLASSNAME = "org.springframework.stereotype.Component"; public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { if (definition instanceof AnnotatedBeanDefinition) { String beanName = determineBeanNameFromAnnotation((AnnotatedBeanDefinition) definition); if (StringUtils.hasText(beanName)) { // Explicit bean name found.return beanName; } } // Fallback: generate a unique default bean name.return buildDefaultBeanName(definition); } /** * Derive a bean name from one of the annotations on the class. * @param annotatedDef the annotation-aware bean definition * @return the bean name, ornull
if none is found */protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef) { AnnotationMetadata amd = annotatedDef.getMetadata(); Settypes = amd.getAnnotationTypes(); String beanName = null; for (String type : types) { Map attributes = amd.getAnnotationAttributes(type); if (isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) { String value = (String) attributes.get("value"); if (StringUtils.hasLength(value)) { if (beanName != null && !value.equals(beanName)) { throw new IllegalStateException("Stereotype annotations suggest inconsistent " + "component names: '" + beanName + "' versus '" + value + "'"); } beanName = value; } } } return beanName; } /** * Check whether the given annotation is a stereotype that is allowed * to suggest a component name through its annotation value()
. * @param annotationType the name of the annotation class to check * @param metaAnnotationTypes the names of meta-annotations on the given annotation * @param attributes the map of attributes for the given annotation * @return whether the annotation qualifies as a stereotype with component name */protected boolean isStereotypeWithNameValue(String annotationType, SetmetaAnnotationTypes, Map attributes) { boolean isStereotype = annotationType.equals(COMPONENT_ANNOTATION_CLASSNAME) || (metaAnnotationTypes != null && metaAnnotationTypes.contains(COMPONENT_ANNOTATION_CLASSNAME)) || annotationType.equals("javax.annotation.ManagedBean") || annotationType.equals("javax.inject.Named"); return (isStereotype && attributes != null && attributes.containsKey("value")); } /** * Derive a default bean name from the given bean definition. * The default implementation simply builds a decapitalized version * of the short class name: e.g. "mypackage.MyJdbcDao" -> "myJdbcDao". *
Note that inner classes will thus have names of the form * "outerClassName.innerClassName", which because of the period in the * name may be an issue if you are autowiring by name. * @param definition the bean definition to build a bean name for * @return the default bean name (never
null
) */protected String buildDefaultBeanName(BeanDefinition definition) { String shortClassName = ClassUtils.getShortName(definition.getBeanClassName()); return Introspector.decapitalize(shortClassName); }復制代碼
Spring在給Bean生成名字的時候,會調用generateBeanName
方法,這個方法會先嘗試獲取注解括號中的名字,也就是用戶自定義的名稱,如果沒有獲取到,則調用buildDefaultBeanName
,用于生成默認的名稱,這個方法會使用Introspector.decapitalize(shortClassName);
,問題就在這個方法上,這個方法的API文檔如下:
public static String decapitalize(String name)Utility method to take a string and convert it to normal Java variable name capitalization. This normally means converting the first character from upper case to lower case, but in the (unusual) special case when there is more than one character and both the first and second characters are upper case, we leave it alone. Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays as "URL". Parameters: name - The string to be decapitalized. Returns: The decapitalized version of the string.
最重要的一句話翻譯過來是說:如果name的開頭兩個及兩個以上字符為大寫,則不作處理并直接返回原來的名字,否則將名稱的首字母小寫后返回。
重命名類型名稱,如原來的MXTable,改成MxTable或者Mxtable等,反正避免開頭兩個字母都是大寫;
getBean的參數(shù)使用MXTable;
在@Component中加上參數(shù),自定義Bean的名稱,如@Component("mxTable")
到此,相信大家對“如何實現(xiàn)Spring中生成Bean時默認生成名稱”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!