本篇内容主要讲解“如何实现Spring中生成Bean时默认生成名称”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何实现Spring中生成Bean时默认生成名称”吧!
定义一个类如下:
@Componentpublic class MXTable{ ...... }复制代码
通过ApplicationContext.getBean("mXTable")
获取这个Bean对象,但是为NULL,导致调用的时候出现空指针异常。
在使用注解生成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, or <code>null</code> if none is found */protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef) { AnnotationMetadata amd = annotatedDef.getMetadata(); Set<String> types = amd.getAnnotationTypes(); String beanName = null; for (String type : types) { Map<String, Object> 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 <code>value()</code>. * @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, Set<String> metaAnnotationTypes, Map<String, Object> 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. * <p>The default implementation simply builds a decapitalized version * of the short class name: e.g. "mypackage.MyJdbcDao" -> "myJdbcDao". * <p>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 <code>null</code>) */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的参数使用MXTable;
在@Component中加上参数,自定义Bean的名称,如@Component("mxTable")
到此,相信大家对“如何实现Spring中生成Bean时默认生成名称”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。