English 中文(简体)
BeanUtils and ConvertUtils
  • 时间:2024-09-08

Java BeanUtils - BeanUtils and ConvertUtils


Previous Page Next Page  

Description

The BeanUtils is defined as a utipty 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;

pubpc class Test {
    @SuppressWarnings("unchecked")
    pubpc 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:

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

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

    pubpc String getAge() {
        return age;
    }
	
    pubpc 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 pbrary that comes with a number of converters to convert to and from different data types and also contain ConvertUtils utipty class which makes use of these converters.

Example

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

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

pubpc class ConvertStringArrayToDoubleArray {
    pubpc 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