This topic is about Java BeanUtils – Basic DynaBeans.
Description
The implementation of BasicDynaBean and BasicDynaClass specifies the capacity of dynamic property to provide the set of properties dynamically. You can start with DynaClass to establish the set of properties. A newInstance() method will create a new DynaBean instances to DynaClass and occupy its initial values as shown in the below example.
Example
The below example shows usage of basic DynaBean implementation:
package com.javadb.apachecommons; import org.apache.commons.beanutils.BasicDynaClass; import org.apache.commons.beanutils.DynaBean; import org.apache.commons.beanutils.DynaClass; import org.apache.commons.beanutils.DynaProperty; public class DynaBeanExample { private final String NR_OF_WHEELS = "numberOfWheels"; private void runExample() { DynaClass dynaClass = new BasicDynaClass("Car", null, new DynaProperty[] { new DynaProperty(NR_OF_WHEELS, Integer.TYPE) }); try { DynaBean car = dynaClass.newInstance(); car.set(NR_OF_WHEELS, 4); System.out.println("Number of wheels: " + car.get(NR_OF_WHEELS)); System.out.println("DynaBean is instance of DynaClass: " + car.getDynaClass().getName()); } catch (IllegalAccessException | InstantiationException ex) { System.err.println(ex.getMessage()); } } public static void main(String[] args) { DynaBeanExample ac = new DynaBeanExample(); ac.runExample(); } }
Output
Let’s carry out the following steps to see how above code works:
- Save the above first code as DynaBeanExample.java.
- Now execute the code using Run option or Ctrl+f11 and output as below gets displayed.
In this topic we learned about Java BeanUtils – Basic DynaBeans. To learn more, Click Here.
Pingback: Java BeanUtils - Background (DynaBeans) - Adglob Infosystem Pvt Ltd