Java BeanUtils Nested Property Access - Java Bean Utils

What is Java BeanUtils Nested Property Access?

Description

You can access the value of nested property of the bean by concatenating the property names of the access path by using "." separators.

You can get and set the values of Nested property by using the below methods:

  • PropertyUtils.getNestedProperty(Object, String)
  • PropertyUtils.setNestedProperty(Object, String, Object)

Parameters:

  • Object: It is a bean whose property to be obtained or modified.
  • String: It is a name of the nested property to be obtained or modified.

Example

In this example, you'll see a way to get and set the values of nested property. we will be creating three classes; SubBean, AppLayer1Bean for beans and BeanUtilsDemo as a main program to run.


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

Create the one more class AppLayer1Bean.java along with the below code:

OutPut

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

  • Save the above first code as BeanUtilsDemo.java.
  • Now execute the code using Run option or Ctrl+f11 and output as below gets displayed.

Java BeanUtils - Nested Property Access


PropertyUtils Method Signatures

The following techniques are provided through the PropertyUtils class, which accepts any arbitrary combinations of simple, indexed and mapped property access to get and set the value of the property of the specified bean.

  • PropertyUtils.getProperty(Object, String)
  • PropertyUtils.setProperty(Object, String, Object)

Parameters:

  • Object: It is a bean whose property to be obtained or modified.
  • String: It is a name of the indexed and/or nested property to be obtained or modified.

Example

The following simple program illustrates the use of getProperty and setProperty methods:

Run the code as specified in the above example and you would get the below output:

Java BeanUtils - Nested Property Access

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Bean Utils Topics