পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Friday, February 3, 2017

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 how 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.
import org.apache.commons.beanutils.PropertyUtils;

public class BeanUtilsDemo {
    public static void main(String args[]){
        try{
            // create the bean
            AppLayer1Bean nested = new AppLayer1Bean();
            // set a SubBean which is part of another bean
            SubBean sb = new SubBean();
            sb.setStringProperty("Hello World from SubBean");
            nested.setSubBean(sb);
  
            // accessing and setting nested properties
            PropertyUtils.setNestedProperty(
                nested, "subBean.stringProperty",
                "Hello World from SubBean, set via Nested Property Access");
   
            System.out.println(
                PropertyUtils.getNestedProperty(nested, "subBean.stringProperty"));
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
}
Now we will create another class called SubBean.java as shown below:
public class SubBean {
    private int intProperty;
    private String stringProperty;
 
    public void setIntProperty(int intProperty) { 
        this.intProperty = intProperty; 
    }
    public int getIntProperty() {
        return this.intProperty; 
    }
 
    public void setStringProperty(String stringProperty) { 
        this.stringProperty = stringProperty; 
    }
    public String getStringProperty() { 
        return this.stringProperty; 
    }
}
Create the one more class AppLayer1Bean.java along with the below code:
public class AppLayer1Bean {
    private SubBean subBean;

    public void setSubBean(SubBean subBean) {
        this.subBean = subBean;
    }
    public SubBean getSubBean(){
        return this.subBean;
    } 
}

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.
nested Property Access

PropertyUtils Method Signatures

The following methods are provided by 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:
import org.apache.commons.beanutils.PropertyUtils;

public class PropertyUtilsTest {
    public static void main(String args[]){
        try{
            Tv Color = new Tv();
            PropertyUtils.setProperty(Color, "color", "Black");
            String value = (String) PropertyUtils.getProperty(Color, "color");
            System.out.println("The color value of Tv is: " + value);
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
    public static class Tv{
        private String color;
      
        public String getColor(){
            return color;
        }
        public void setColor(String color){
            this.color = color;
        }
    }
}
Run the code as specified in the above example and you would get the below output:
Nested Property Access

No comments:

Post a Comment