Java Propertyutils Event - Getting As Well As Setting Properties Past Times Name
Sunday, November 11, 2018
Add Comment
PropertyUtils course of report of Apache commons beanutils library is really useful as well as provides you lot mightiness to modify properties of Java object at runtime. PropertyUtils enables you lot to write highly configurable code where you lot tin sack render advert of edible bean properties as well as at that spot values from configuration rather than coded inward Java program as well as Apache PropertyUtils tin sack laid those properties on Java object at runtime. One pop event of how powerful PropertyUtils tin sack survive is display tag which provides rich tabular display for JSP pages, it uses PropertyUtils course of report to instruct values of object at runtime as well as than display it. You tin sack setup properties every bit column as well as exclusively selected columns volition survive displayed. Even larger spider web framework similar Struts as well as Spring too uses Apache common beanutils library for getting as well as setting coffee properties past times name.
PropertyUtils is based on Java reflection but provides a convenient method to operate on Java object at runtime. By using PropertyUtils you lot tin sack instruct a map of all Object properties which enables you lot to modify them at runtime past times values coming from all the places similar web request, database or configuration files. In this Java tutorial nosotros volition event of how to instruct as well as laid properties of coffee object using PropertyUtils course of report at runtime.
This article is inward continuation of my before postal service on opened upwardly root library similar How to boundary pose out of user session inward spider web application using Spring Security as well as How to perform LDAP authentication on windows Active directory using Spring Security. If you lot haven’t read them already you lot may uncovering them useful as well as interesting.
Java Program to instruct object properties at runtime
Here is a uncomplicated Java programme which shows how to role PropertyUtils course of report to modify object properties or to instruct Object as well as its properties at runtime. Remember nosotros don’t know anything virtually Object at compile time. On runtime Object is provided to programme along amongst advert of belongings to think or modify:
import org.apache.commons.beanutils.PropertyUtils;
public class PropertyUtilsTest {
public static void main(String args[]) {
try{
MobilePhone flexiColor = new MobilePhone();
//here color as well as bluish strings tin sack come upwardly from multifariousness or sources
//e.g. configuration files, database, whatsoever upstream arrangement or via HTTP Request
PropertyUtils.setProperty(flexiColor, "color", "blue");
String value = (String) PropertyUtils.getProperty(flexiColor, "color");
System.out.println("PropertyUtils Example belongings value: " + value);
}catch(Exception ex){
ex.printStackTrace();
}
}
public static class MobilePhone {
private String brand;
private String color;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
}
public class PropertyUtilsTest {
public static void main(String args[]) {
try{
MobilePhone flexiColor = new MobilePhone();
//here color as well as bluish strings tin sack come upwardly from multifariousness or sources
//e.g. configuration files, database, whatsoever upstream arrangement or via HTTP Request
PropertyUtils.setProperty(flexiColor, "color", "blue");
String value = (String) PropertyUtils.getProperty(flexiColor, "color");
System.out.println("PropertyUtils Example belongings value: " + value);
}catch(Exception ex){
ex.printStackTrace();
}
}
public static class MobilePhone {
private String brand;
private String color;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
}
Output:
PropertyUtils Example belongings value: blue
Benefits of using Apache common PropertyUtils:
PropertyUtils provides lot of flexibility piece writing programme though that comes amongst the cost of reflection; hither are merely about of the of import benefits of using Apache common PropertyUtils I tin sack intend of:
1) You tin sack instruct whatsoever belongings value at runtime without having coded for it e.g. bean.getOrder() tin sack survive replaced by
PropertyUtils.getProperty(bean, order) where edible bean as well as lodge are runtime value as well as tin sack change.
2) Similarly, you lot tin sack laid whatsoever object belongings at runtime e.g. bean.setOrder(order) tin sack survive replaced by
PropertyUtils.setProperty(bean, order, new Order())
3) Apache beanUtils enables you lot to write highly configurable code.
Errors as well as Exception piece using PropertyUtils
If you lot get to laid a belongings as well as getting "Exception inward thread "main" java.lang.NoSuchMethodException: Property 'color' has no setter method" similar below:
Exception inward thread "main" java.lang.NoSuchMethodException: Property 'color' has no setter method at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:1746)
at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1648)
at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1648)
There could survive dissimilar reasons for this exceptions e.g.
1) Bean course of report whose belongings you lot are setting is non public. Yes, you lot instruct this Exception fifty-fifty if the course of report is accessible similar Inner course of report but non world rather it’s private, default or protected.
2) There is no setter method for that property.
3) Setter method for that belongings is private.
You volition too instruct "java.lang.IllegalArgumentException: declaration type mismatch" similar below if you lot are passing wrong type for declaration or your declaration type is non convertible on what method is expecting e.g. if a belongings is of type int as well as you lot are passing String.
java.lang.IllegalArgumentException: declaration type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Dependency:
commons-beanutils.jar
commons-logging.jar
commons-collections.jar
In lodge to role PropertyUtils class, you lot yell for to include commons-beanutils.jar inward your Java Classpath. as well as since commons-beanutils.jar too has a dependency on commons-logging.jar you lot yell for to include that every bit well. If you lot are using Maven for edifice your projection or maintaining dependency you lot tin sack include the next dependency.
Maven dependency for apache common beanutils
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.2</version>
</dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.2</version>
</dependency>
That’s all on How to role PropertyUtils from Apache commons-beanutils.jar to instruct as well as laid object properties at runtime. Apart from PropertyUtils beanutils too contains several other utility for dealing amongst beans inward Java application.
Further Learning
Spring Framework 5: Beginner to Guru
How to defined Error page inward JSP
Reference
http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/PropertyUtils.html
0 Response to "Java Propertyutils Event - Getting As Well As Setting Properties Past Times Name"
Post a Comment