tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Jodd > Beans > How to get declared bean property value by name

How to get declared bean property value by name 

Jodd is an open-source java library with lot of reusable components and feature rich utilities. This requires the library jodd-3.3.2.jar to be in classpath. The following example shows how to get declared bean property value given the property name using BeanUtil.getDeclaredProperty() API. If a getter method exists for a given property then it uses the same otherwise accesses the field directly.

File Name  :  
com/bethecoder/tutorials/jodd/common/Student3.java 
   
package com.bethecoder.tutorials.jodd.common;

public class Student3 {

  private String name;
  private int age;
  private String hobby;

  public Student3(String name, int age, String hobby) {
    super();
    this.name = name;
    this.age = age;
    this.hobby = hobby;
  }

  public String getName() {
    System.out.println("In getName method with name : " + name);
    return name;
  }
  
}
   

File Name  :  
com/bethecoder/tutorials/jodd/beans/GetDeclaredPropertyTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.jodd.beans;

import jodd.bean.BeanUtil;

import com.bethecoder.tutorials.jodd.common.Student3;

public class GetDeclaredPropertyTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    
    Student3 std = new Student3("Sriram"2"Chess");
    
    String name = (StringBeanUtil.getDeclaredProperty(std, "name");
    System.out.println(name);
    
    Integer age = (IntegerBeanUtil.getDeclaredProperty(std, "age");
    System.out.println(age);
    
    String hobby = (StringBeanUtil.getDeclaredProperty(std, "hobby");
    System.out.println(hobby);
  }

}
   

It gives the following output,
In getName method with name : Sriram
Sriram
2
Chess



 
  


  
bl  br