English 中文(简体)
Basic DynaBeans
  • 时间:2024-12-22

Java BeanUtils - Basic DynaBeans


Previous Page Next Page  

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 estabpsh 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;

pubpc 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());
      }

   }
   pubpc 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.

BasicDynaBean and BasicDynaClass Advertisements