Five ‘s Weblog

October 24, 2007

org.apache.commons.beanutils

Filed under: Programming — by powerdream5 @ 10:53 pm
Tags: , ,

10_24_1_2007.jpg

       Today, I found a userful utility classes package when I read my former notes. It is org.apache.commons.beanutils, one of the packages of Jakarta commons. We call the org.apache.commons.beanutils package Bean Introspection Utilities. When we use setter to initialize the properties of a Javabean, especially which has a lot of properties, we always complain that there are many setter methods needed to write. Fortunately, with the help of the org.apache.commons.beanutils, we can just use one sentence to realize that. The following is an example: (you need to download the package firstly)

//User.java a simple javabean
public class User{
        private String username;
        private String password;
        private int age;

        //the setter, getter methods
        public String getUsername(){
               return username;
        }
        public void setUsername(String username){
              this.username = username;
        }
       …omit to save space …
}

import java.util.HashMap;
//import the necessary class
import org.apache.commons.beanutils.BeanUtils;

public class Test{
 
      public static void main(String[] args){

              //in order to show this example, I create a map
             //if this class is a servelet, we can use request.getParameterMap()
             //get all the properties of the form stored in a map

             HashMap map = new HashMap();
             map.put(“username”,”powerdream5″);
             map.put(“password”,”5maerdrewop”);
             map.put(“age”,”23″);
 
            User user = new User();
            try{
                  BeanUtils.populate(user,map);
            }catch(Exception e){
                  e.printStackTrace();
            }

           System.out.println(“Username:”+user.getUsername());
           System.out.println(“Password:”+user.getPassword());
           System.out.println(“Age:”+user.getAge());
       }
}

       populate(java.lang.object bean, java.util.Map properties) is a static method. This method can do some basic type conversions such as from String to int. However, if there are a mismatch with the type, and cannot be converted, an exception will be thrown.

10_24_2_2007.jpg

       Using this package relly makes things easy. However, some people point that the performance of this package is not good, whichis not as good as using the setter method directly. In fact, we can find the factors which influence the performance. The org.apache.commons.beanutils package is based on the mechanism of reflection. Therefore, before we can assign value to the properties, we have to introspect the bean class to find proper properties, then invoke the setter methods. Compared with using setter methods directly, this method really take a detour. However, if you don’t care a little loss in the performance of your application, and want to save some effort, using this package does no harm!

10_24_3_2007.jpg

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: