Spring @Service generation rules bean name

Today encounter a problem, write a @Service of bean, the class name roughly: BKYInfoServcie.java

dubbo export services configuration:

<dubbo:service interface="com.xxx.XxxService" ref="bKYInfoServcie" />

The results start error: Can not find the bean bKYInfoServcie

bean name is not I expected "bKYInfoServcie", temporarily designated bean name became bKYInfoServcie to solve, namely: @Service ( "bKYInfoServcie")

 

But still I feel rather strange, always thought before the default name of Spring bean annotation process is to form the first letter lowercase, and then stitching the back of the characters, but today it seems not the case.

Back turned a bit original code, the original there is another of a special treatment: when the class name begins with a capital letter or two more words, bean name will be consistent with the class name

 

 

Copy the code
Copy the code
/**
     * 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})
     */
    protected String buildDefaultBeanName(BeanDefinition definition) {
        String shortClassName = ClassUtils.getShortName(definition.getBeanClassName());
        return Introspector.decapitalize(shortClassName);
    }
Copy the code
Copy the code

 

Copy the code
Copy the code
    /**
     * 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.
     * <p>
     * Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays
     * as "URL".
     *
     * @param  name The string to be decapitalized.
     * @return  The decapitalized version of the string.
     */
    public static String decapitalize(String name) {
        IF (name == null || name.length () == 0) {
            name return; 
        } 
    // If the first two classes are in uppercase characters found, it returns the class name directly IF (name.length ()>. 1 && Character.isUpperCase (name.charAt (. 1)) && Character.isUpperCase (name .charAt (0))) { return name; }
    // the name of the class is the first letter lowercase turn, and then return char chars [] = name.toCharArray (); chars [0] = Character.toLowerCase (chars [ 0]); return new new String (chars); }
Copy the code
Copy the code

 

Original Address: https: //www.cnblogs.com/kevin-yuan/p/5437140.html

Guess you like

Origin www.cnblogs.com/renjiaqi/p/11577287.html