JavaBean specification on a few you should know

As a Java programmer, for JavaBean Perhaps you will be very familiar with; its role in the multi-layer system, the name has a different PO, VO, DTO, POJO, DO (Domain Object). However, it is a Class, put on some properties and their setter/getter methods. Although we now rarely to  EJB 2.0 and JavaBean mixed, but why does the IDE generated for the property getter/setter methods, the application run-time, there can not find a bean property setter or getter method?

In Sun’s Web site on the JavaBean specification PDF document has 114 pages. Inevitably some of the rules a bit strange; so it is difficult to deal with some IDE, so we need to understand some of them, to regulate our JavaBean and explain some situations.

JavaBean specification:
http://java.sun.com/javase/technologies/desktop/javabeans/docs/spec.html

Practical problems

First, JavaBean properties and the setter/getter methods. Lowercase letters in the beginning of property name , the corresponding getter/setter methods get/set connected to the first letter capitalized property name. In most cases is correct, and the current popular IDE (Eclipse, JBuilder) are also recognized. But if encountered some code left over from non-standard attribute names, or other reasons, then you still need to study.

Eclipse for a few of properties generated getter/setter methods.

sName (turn from the C, and that Name is a string, so add prefix s)          getSName ()/setSName(String name)
URL (Usual is abbreviations/proper nouns, it should be all capital letters)     getURL()/setURL(String url)

Above the first Eclipse generated by getSName()/setSName(String name) method, reference JavaBean specification, which is wrong. If such an method, we use the Tags(such as Struts Tags, ), or to Hibernate/iBatis mapping, it will show sName attributes can not find getter/setter methods error. Should be the correct version of getsName() and setsName(String name).

Properties and access rules

Create JavaBean properties, Important: Abbreviations are as an independent word, not a single letter. For example, URL of the corresponding Properties name should be url, corresponding method getUrl()/setUrl().

Specification is another special place, the second for the upper case letters to distinguish between the property name. If the property name of the second letter is capitalized, then the properties are directly used as a getter/setter methods get/set the latter part, that is the same case. This is why access to the corresponding sName is getsName()/setsName() reasons. Take a look at the specification table below:

Property name/type getter method setter method
xcoordinate/Double public DoublegetXcoordinate() public void setXcoordinate
(Double newValue)
xCoordinate/Double public DoublegetxCoordinate() public void setxCoordinate
(Double newValue)
XCoordinate/Double public DoublegetXCoordinate() public void setXCoordinate
(Double newValue)
Xcoordinate/Double Not allowed Not allowed
student/Boolean public Boolean getStudent() public void setStudent
(Boolean newValue)
student/boolean public boolean getStudent()
public boolean isStudent()
public void setStudent
(boolean newValue)

Property name is the first letter capitalized, the next letter is lowercase, you will not find its getter/setter methods, the use of this propertyis name wrong. For boolean type attribute getter method is isXxx() or getXxx() can determine their own.

There is also a little touch we are on the index property getter/setter methods, for example:

private  OrderItem[] orderItem;

Besides has getter/setter, with the index parameters have two versions, as follows:

public OrderItem[] getOrderItem();
public void setOrderItem(OrderItem[] newArray);
public OrderItem[] getOrderItem(int index);
public void setOrderItem(int index, OrderItem orderItem);

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

No Responses to “JavaBean specification on a few you should know”

Leave a Reply

Name:
Email:
Website:
Comment:
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>