数据加载中……
Spring配置文件中id的第二个字母不能大写问题

       今天遇到一个问题,在spring配置文件中的id第二个字母不能大写,否则会产生异常:Bean property 'kManager' is not writable or has an invalid setter method. Did you mean 'KManager'?.为了解决问题研究了一下问题的原因:spring在autoWire的同时用到了jdk提供的java.beans.*目录下的类(等有时间好好研究一下这些类,功能很强大),通过它们能够得到bean的详细信息。其中有个类PropertyDestriptor类能够通过bean中的set/get方法找到property,不过有个小前提:property的命名要遵循第二个字母不能大写。因为java是国外开发的,它对命名遵循了英语的一个规范:大部分的单词第二个字母都是小写的,除了URL之类的单词。
        下面的java.beans.Introspector类中通过set/get方法找到property的代码(希望大家可以自己去看看java.beans.*的代码)。Open Source! I love it.

/**
     * 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);
    }


posted on 2007-12-05 14:27 牛浪de流 阅读(782) 评论(0)  编辑  收藏 所属分类: 爪哇学习


只有注册用户登录后才能发表评论。


网站导航: