Standard JavaBeans
- Suppressing Properties
- Customizing Introspection
- Nested Property Access
- Basic Property Access
- Background
Dynamic Beans (DynaBeans)
Data Type Conversions
Utility Objects & Classes
Collections
Java BeanUtils Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java BeanUtils - Basic Property Access
Description
You can access the basic properties by using the following ways:
Simple Property
Indexed Property
Mapped Property
Simple Property
You can get and set the simple property values by using the below API signatures:
PropertyUtils.getSimpleProperty(Object, String)
PropertyUtils.SetSimpleProperty(Object, String, Object)
Parameters:
Object: It is a bean object that specifies the bean property to be extracted.
String: It is a string name that specifies the name of the property to be extracted.
Indexed Property
You can use two options for creating indexed properties; first option is building the subscript into property name and second option is defining the subscript in a separate argument to call the method.
The indexed properties can be get and set by using the below methods:
PropertyUtils.getIndexedProperty(Object, String)
PropertyUtils.getIndexedProperty(Object, String, int)
PropertyUtils.setIndexedProperty(Object, String, Object)
PropertyUtils.setIndexedProperty(Object, String, int, Object)
Parameters:
Object: It is a bean object that specifies the bean property to be extracted.
String: It is a string name that specifies the name of the property to be extracted.
int: It sets an index of the property value.
Object: It specifies the value for an indexed property element.
Mapped Property
You can get and set the mapped property values by using the below API signatures. If you have any extra argument, then it can be written within parentheses as ("(" and ")") instead of using square brackets.
PropertyUtils.getMappedProperty(Object, String)
PropertyUtils.getMappedProperty(Object, String, String)
PropertyUtils.setMappedProperty(Object, String, Object)
PropertyUtils.setMappedProperty(Object, String, String, Object)
Parameters:
Object: It is a bean object that specifies the bean property to be extracted.
String: It is a name of the property value that should be set for Mapped property.
String: It defines the key of the property value to be set.
Object: It specifies the value of property to be set.
Example
The below example demonstrates use of above properties in the beanUtils:
import org.apache.commons.beanutils.PropertyUtils; import java.util.ArrayList; import java.util.List; pubpc class BeanUtilsPropertyDemo{ pubpc static void main(String args[]){ try{ // Creating the bean and allows to access getter and setter properties MyBean myBean = new MyBean(); // Setting the properties on the myBean PropertyUtils.setSimpleProperty(myBean, "stringProp", "Hello!This is a string"); PropertyUtils.setSimpleProperty(myBean, "floatProp", new Float(25.20)); // Getting the simple properties System.out.println("String Property: " + PropertyUtils.getSimpleProperty(myBean, "stringProp")); System.out.println("Float Property: " + PropertyUtils.getSimpleProperty(myBean, "floatProp")); // Here we will create a pst for the indexed property Listpst = new ArrayList (); pst.add("String value 0"); pst.add("String value 1"); myBean.setListProp(pst); // get and set this indexed property PropertyUtils.setIndexedProperty(myBean, "pstProp[1]", "This is new string value 1"); System.out.println("List Property[1]: " + PropertyUtils.getIndexedProperty(myBean, "pstProp[1]")); }catch(Exception e){ System.out.println(e); } } }
Now we will create one more class called MyBean.java for the bean class:
import java.util.ArrayList; import java.util.List; pubpc class MyBean { private String stringProp; private float floatProp; //indexed property @SuppressWarnings("rawtypes") private List pstProp = new ArrayList(); pubpc void setStringProp(String stringProp) { this.stringProp = stringProp; } pubpc String getStringProp() { return this.stringProp; } pubpc void setFloatProp(float floatProp) { this.floatProp = floatProp; } pubpc float getFloatProp() { return this.floatProp; } pubpc void setListProp(List<?> pstProp) { this.pstProp = pstProp; } pubpc List<?> getListProp() { return this.pstProp; } }
Output
Let s carry out the following steps to see how above code works:
Save the above first code as BeanUtilsPropertyDemo.java.
Now execute the code using Run option or Ctrl+f11 and output as below gets displayed.