您的位置:首页 > 编程语言 > Java开发

Java Bean属性命名规范问题分析

2012-03-31 22:57 323 查看
2012-03-20 10:09 成家荣 成家荣的博客 我要评论(0) 字号:T | T





Eclipse根据这种方式生成getter访问器和setter设值器, 由于属性名isShowCode的is没有去掉, 以致java bean类违背了java bean 的命名规范。

AD:

问题由来:

最近在一个java bean类中定义了一个boolean类型的变量:
//boolean属性:是否显示
private boolean isShowCode ;

//使用Eclipse自动生成getter/setter方法如下:
public boolean isShowCode() {
return isShowCode;
}
public void setShowCode(boolean isShowCode) {
this.isShowCode = isShowCode;
}


spring在给java bean 设置值的时候, 抛出异常:
Caused by: org.springframework.beans.NotWritablePropertyException:
Invalid property 'isShowCode' of bean class [com.codemouse.beans.Country]:
Bean property 'isShowCode' is not writable or has an invalid setter method.
Did you mean 'showCode'?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1064)


代码运行环境: jdk 1.6 + eclipse 3.2 + spring 3.1, 本文下面的代码都在该环境下测试。

原因跟踪分析:跟踪Spring源码, 可以看到下面这段代码:
private CachedIntrospectionResults(Class beanClass,
boolean cacheFullMetadata) throws BeansException {
... ...
this.beanInfo = new ExtendedBeanInfo(Introspector.getBeanInfo(beanClass));
... ...
}


方法Introspector.getBeanInfo(beanClass)返回的时候,获取到的java bean 信息中的isShowCode属性的名称已经被改成了"showCode"。





到这里可以确定问题不是出在spring代码中, 网上有不少帖子说是spring的处理规则导致了这个问题,这里是不是可以否定这种看法?

问题跟踪到这,也即跟踪到了java.beans包。尝试继续跟踪JDK源码,可能由于我的JDK的jar包和源码不匹配的原因, eclipse总是监控不到中间变量。也就没有再跟踪进去了。可以知道的是,javabean中 的isShowCode 属性 和 对应的getter/setter方法应该是没有遵循javabean规范。Eclipse自动生成的getter/setter方法看来也是存在一些问题的。

Eclipse自动生成boolean类型属性的方法是不是有点奇怪呢? 属性 isShowCode 的getter访问器是isShowCode()而不是getIsShowCode(), setter设值器是setShowCode()而不是setIsShowCode()。原来在java bean 规范关于中提到, boolean属性<propertyName>的getter访问器可以使用下面这种模式

public boolean is<PropertyName>(){...};

来代替

public boolean get<PropertyName>(){...};

Javabean 规范(下载链接:http://download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/ ) 在8.3 章节"Design Patterns for Properties" 中的描述:





Eclipse根据这种方式生成getter访问器和setter设值器, 由于属性名isShowCode的is没有去掉, 以致java bean类违背了java bean 的命名规范。

JavaBean 的属性名和getter/setter存取方法规则小结:

1. 对于常规属性 <propertyName> , 属性名称的第一个单词小写且字母个数大于1,第二个单词首字母大写 。对应的getter/setter方法名为:get /set + <PropertyName>(), 即属性名称的第一个单词的首字母改成大写, 前面再加上"get"或"set"前缀。

2. 对于布尔类型 <propertyName> , 可以按常规属性的规则编写getter/setter方法外, getter方法可以使用 is + <PropertyName>()的形式来代替。

3. 对于非常规属性<pName>, 属性名称的第一个单词小写且字母个数等于1,第二个单词首字母大写 。

3.1 ) 对应的getter/setter方法名可以为:get/set + <PName>(), 即第一个单词的首字母为改为大写,前面再加上"get"或"set"前缀。Eclipse3.2 按这种方式自动生成getter/setter方法。代码片段:
<bean id="country" class="com.codemouse.beans.Country" lazy-init="true">
<property name="pName">
<value>中国</value>
</property>
<property name="code">
<value>CN</value>
</property>
<property name="showCode">
<value>true</value>
</property>
</bean>

private String pName;

public String getPName() {
return pName;
}
public void setPName(String name) {
pName = name;
}


3.2 )对应的getter/setter方法名也可以为:get/set+ <pName>(), 即属性名称不变,第一个单词的首字母任然为小写,前面再加上"get"或"set"前缀。这种方式也可以正常运行。网上有帖子说Eclipse3.5按这种方式自动生成getter/setter方法。

代码片段:
   <bean id="country" class="com.codemouse.beans.Country" lazy-init="true">
<property name="pName">
<value>中国</value>
</property>
<property name="code">
<value>CN</value>
</property>
<property name="showCode">
<value>true</value>
</property>
</bean>

private String pName;

public String getpName() {
return pName;
}
public void setpName(String name) {
pName = name;
}


4. 对于非常规属性<PName>, 属性名称的前两个字母都是大写 。即连续两个大写字母开头的属性名。

对应的getter/setter方法名为: get/set + <PName>(), 即属性名称不变,前面再加上"get"或"set"前缀。

spring3.1 配置文件代码片段:
<bean id="country" class="com.codemouse.beans.Country" lazy-init="true">
<property name="PName">
<value>中国</value>
</property>
<property name="code">
<value>CN</value>
</property>
<property name="showCode">
<value>true</value>
</property>
</bean>

private String PName;

public String getPName() {
return PName;
}
public void setPName(String name) {
PName = name;
}


5. 对于非常规属性<Property>或<PropertyName>, 属性名称第一个字母大写 。网上有帖子说这是不符合JSR规范的,会报 "属性找不到" 的错误。

(如帖子1: http://lzh166.iteye.com/blog/631838

帖子2: http://hi.baidu.com/w8y56f/blog/item/4fd037e845bbbe372cf5342a.html)。我在我的 环境下测试了下, 是不会报错的,可以正常运行,虽然这种命名方式是令人难以忍受的:
<bean id="country" class="com.codemouse.beans.Country" lazy-init="true">
<property name="PropertyName">
<value>中国</value>
</property>
<property name="code">
<value>CN</value>
</property>
<property name="showCode">
<value>true</value>
</property>
<property name="Xcoordinate">
<value>12.345</value>
</property>
</bean>

private String PropertyName;
public String getPropertyName() {
return PropertyName;
}
public void setPropertyName(String propertyName) {
PropertyName = propertyName;
}

private Double Xcoordinate;
public Double getXcoordinate() {
return Xcoordinate;
}
public void setXcoordinate(Double xcoordinate) {
Xcoordinate = xcoordinate;
}


测试方法: 第一个@test方法用普通javabean调用方式测试; 第二个@test方法使用spring创建bean
@Test
public void testJavaBeanNamingRule0(){
Country country = new Country();
country.setPropertyName("中国");
country.setXcoordinate(Double.valueOf(123.456f));
System.out.println(country.getPropertyName());
System.out.println(country.getXcoordinate());
}

@Test
public void testJavaBeanNamingRule(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("myBeans.xml");
Country country = (Country)ctx.getBean("country");
System.out.println(country.getPropertyName());
System.out.println(country.getXcoordinate());
}


运行结果:都可以正常运行。
中国
123.45600128173828
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
中国
12.345


总结:

1. javabean属性命名尽量使用常规的驼峰式命名规则

2. 属性名第一个单词尽量避免使用一个字母:如eBook, eMail。

3. boolean属性名避免使用 “is” 开头的名称

4. 随着jdk, eclipse, spring 等软件版本的不断提高, 底版本的出现的问题可能在高版本中解决了, 低版本原来正常的代码可能在高版本环境下不再支持。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: