Spring @Service生成bean名称的规则

今天碰到一个问题,写了一个@Service的bean,类名大致为:BKYInfoServcie.java

dubbo export服务的配置:

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

结果启动报错:找不到名为bKYInfoServcie的bean

bean的名字不是我预期的"bKYInfoServcie",临时将bean的名字指定成了bKYInfoServcie来解决的,即:@Service("bKYInfoServcie")

 

但还是觉得比较奇怪,之前一直以为Spring对注解形式的bean的名字的默认处理就是将首字母小写,再拼接后面的字符,但今天看来不是这样的。

回来翻了一下原码,原来还有另外的一个特殊处理:当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致

 

 

复制代码
复制代码
/**
     * 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);
    }
复制代码
复制代码

 

复制代码
复制代码
    /**
     * 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) {
            return name;
        }
    // 如果发现类的前两个字符都是大写,则直接返回类名 if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) && Character.isUpperCase(name.charAt(0))){ return name; }
    // 将类名的第一个字母转成小写,然后返回 char chars[] = name.toCharArray(); chars[0] = Character.toLowerCase(chars[0]); return new String(chars); }
复制代码
复制代码

 

原文地址:https://www.cnblogs.com/kevin-yuan/p/5437140.html

转载于:https://www.cnblogs.com/renjiaqi/p/11577287.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值