Java BeanUtils - BeanUtils and ConvertUtils


Advertisements


Description

The BeanUtils is defined as a utility method for populating JavaBeans properties and ConvertUtils method converts string scalar values to objects, string arrays to arrays of the specified class.

BeanUtils

The BeanUtils accepts string values by using the setter methods and automatically converts them to suitable property types for Java primitives and uses the getter methods for reverse conversion. The populate() method accepts set of property values from java.util.HashMap and uses the suitable setters whenever bean contain the property with the same name.

Example

The below example shows usage of BeanUtils properties:

import java.util.HashMap;
import org.apache.commons.beanutils.BeanUtils;

public class Test {
    @SuppressWarnings("unchecked")
    public static void main(String[] args){
        @SuppressWarnings("rawtypes")
        HashMap map = new HashMap();
        map.put("username","admin");
        map.put("password","secret");
        map.put("age","52");
        
        User bean = new User();
        try{
              BeanUtils.populate(bean,map);
        }catch(Exception e){
              e.printStackTrace();
        }
        
        System.out.println("Username: "+bean.getUsername());
        System.out.println("Password: "+bean.getPassword());
        System.out.println("Age: "+bean.getAge());
    }
}

Now we will create another class called User.java as shown below:

public class User {
    private String username;
    private String password;
    private String age;
    
    public String getUsername(){
        return username;
    }
    
    public void setUsername(String username){
        this.username = username;
    }

    public String getPassword() {
        return password;
    }
	
    public void setPassword(String password){
        this.password = password;
    }

    public String getAge() {
        return age;
    }
	
    public void setAge(String age){
        this.age = age;
    }
}

Output

Let's carry out the following steps to see how above code works:

  • Save the above first code as Test.java.

  • Now execute the code using Run option or Ctrl+f11 and output as below gets displayed.

 BeanUtils and ConvertUtils

ConvertUtils

The Apache Commons BeanUtils is a library that comes with a number of converters to convert to and from different data types and also contain ConvertUtils utility class which makes use of these converters.

Example

The below example shows the conversion of string array to a double array using ConvertUtils utility:

package com.javadb;
import org.apache.commons.beanutils.ConvertUtils;

public class ConvertStringArrayToDoubleArray {
    public static void main(String[] args) {
        String values[] = { "5", "6", "3" };
        double[] doubleValues = (double[])ConvertUtils.convert(values, Double.TYPE);   
        for (double d : doubleValues) {
            System.out.println(d);
        }
    }
}

Output

  • Save the above first code as ConvertStringArrayToDoubleArray.java.

  • Now execute the code using Run option or Ctrl+f11 and output as below gets displayed.

ConvertUtils

Advertisements