In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to achieve the default generation name when generating Bean in Spring". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "how to achieve the default generation name when generating Bean in Spring"!
Problem scenario:
Define a class as follows:
@ Componentpublic class MXTable {.} copy the code
Get this Bean object through ApplicationContext.getBean ("mXTable"), but it is NULL, resulting in a null pointer exception on the call.
The cause of the problem:
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 null if none is found * / protected String determineBeanNameFromAnnotation (AnnotatedBeanDefinition annotatedDef) {AnnotationMetadata amd = annotatedDef.getMetadata (); Set types = 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, Set metaAnnotationTypes 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);} copy the code
When Spring generates a name for Bean, it calls the generateBeanName method. This method first tries to get the name in the comment parentheses, that is, the user-defined name. If it does not get it, it calls buildDefaultBeanName to generate the default name, and this method uses Introspector.decapitalize (shortClassName). The problem with this method is that the API document for this method is as follows:
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.
The most important sentence is that if the first two or more characters of name are uppercase, no processing will be done and the original name will be returned directly, otherwise the first letter of the name will be returned in lowercase.
Solution method
Rename the type name, such as the original MXTable, change it to MxTable or Mxtable, etc., anyway, avoid capitalizing the first two letters.
The parameters of getBean use MXTable
Add a parameter to @ Component to customize the name of Bean, such as @ Component ("mxTable")
At this point, I believe you have a deeper understanding of "how to achieve the default generated name when generating Bean in Spring". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.