`
xiaotian_ls
  • 浏览: 299730 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

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

分享到:
评论
2 楼 fireinwind 2010-02-10  
harman001 写道
,为什么我的id大写同样没事啊!- -!



在struts中调用调用Spring的注入,无论运用手工注入还是自动注入,都是可以找到此类的.

在单元测试中,testcase 调用手工注入方式是可以找到此类的.
在单元测试中,testcase 调用自动注入方式是找不到此类的
1 楼 harman001 2009-07-03  
,为什么我的id大写同样没事啊!- -!

相关推荐

Global site tag (gtag.js) - Google Analytics