Java Propertyutils Event - Getting In Addition To Setting Properties Past Times Name
Java Propertyutils Event - Getting In Addition To Setting Properties Past Times Name - Hallo sahabat BEST LEARNING JAVA, Pada Artikel yang anda baca kali ini dengan judul Java Propertyutils Event - Getting In Addition To Setting Properties Past Times Name, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan
Artikel core java,
Artikel jsp-servlet, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.
Judul : Java Propertyutils Event - Getting In Addition To Setting Properties Past Times Name
link : Java Propertyutils Event - Getting In Addition To Setting Properties Past Times Name
Further Learning
Spring Framework 5: Beginner to Guru
How to defined Error page inward JSP
Judul : Java Propertyutils Event - Getting In Addition To Setting Properties Past Times Name
link : Java Propertyutils Event - Getting In Addition To Setting Properties Past Times Name
Java Propertyutils Event - Getting In Addition To Setting Properties Past Times Name
PropertyUtils shape of Apache commons beanutils library is real useful in addition to provides y'all mightiness to modify properties of Java object at runtime. PropertyUtils enables y'all to write highly configurable code where y'all tin dismiss furnish lift of edible bean properties in addition to at that topographic point values from configuration rather than coded inward Java program in addition to Apache PropertyUtils tin dismiss laid those properties on Java object at runtime. One pop representative of how powerful PropertyUtils tin dismiss hold upwards is display tag which provides rich tabular display for JSP pages, it uses PropertyUtils shape to instruct values of object at runtime in addition to than display it. You tin dismiss setup properties equally column in addition to alone selected columns volition hold upwards displayed. Even larger spider web framework similar Struts in addition to Spring too uses Apache common beanutils library for getting in addition to setting coffee properties past times name.
PropertyUtils is based on Java reflection exactly provides a convenient method to operate on Java object at runtime. By using PropertyUtils y'all tin dismiss instruct a map of all Object properties which enables y'all to alter them at runtime past times values coming from all the places similar web request, database or configuration files. In this Java tutorial nosotros volition representative of how to instruct in addition to laid properties of coffee object using PropertyUtils shape at runtime.
This article is inward continuation of my before postal service on opened upwards beginning library similar How to limit break of user session inward spider web application using Spring Security in addition to How to perform LDAP authentication on windows Active directory using Spring Security. If y'all haven’t read them already y'all may honour them useful in addition to interesting.
Java Program to instruct object properties at runtime
Here is a uncomplicated Java programme which shows how to role PropertyUtils shape to alter object properties or to instruct Object in addition to its properties at runtime. Remember nosotros don’t know anything almost Object at compile time. On runtime Object is provided to programme along alongside lift 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 in addition to bluish strings tin dismiss come upwards from diverseness or sources
//e.g. configuration files, database, whatever upstream organization 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 in addition to bluish strings tin dismiss come upwards from diverseness or sources
//e.g. configuration files, database, whatever upstream organization 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 alongside the cost of reflection; hither are about of the of import benefits of using Apache common PropertyUtils I tin dismiss intend of:
1) You tin dismiss instruct whatever belongings value at runtime without having coded for it e.g. bean.getOrder() tin dismiss hold upwards replaced by
PropertyUtils.getProperty(bean, order) where edible bean in addition to enterprise are runtime value in addition to tin dismiss change.
2) Similarly, y'all tin dismiss laid whatever object belongings at runtime e.g. bean.setOrder(order) tin dismiss hold upwards replaced by
PropertyUtils.setProperty(bean, order, new Order())
3) Apache beanUtils enables y'all to write highly configurable code.
Errors in addition to Exception piece using PropertyUtils
If y'all endeavour to laid a belongings in addition to 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 hold upwards dissimilar reasons for this exceptions e.g.
1) Bean shape whose belongings y'all are setting is non public. Yes, y'all instruct this Exception fifty-fifty if the shape is accessible similar Inner shape exactly 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 y'all 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 in addition to y'all 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 enterprise to role PropertyUtils class, y'all postulate to include commons-beanutils.jar inward your Java Classpath. in addition to since commons-beanutils.jar too has a dependency on commons-logging.jar y'all postulate to include that equally well. If y'all are using Maven for edifice your projection or maintaining dependency y'all tin dismiss 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 in addition to laid object properties at runtime. Apart from PropertyUtils beanutils too contains several other utility for dealing alongside 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
Demikianlah Artikel Java Propertyutils Event - Getting In Addition To Setting Properties Past Times Name
Sekianlah artikel Java Propertyutils Event - Getting In Addition To Setting Properties Past Times Name kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.
Anda sekarang membaca artikel Java Propertyutils Event - Getting In Addition To Setting Properties Past Times Name dengan alamat link https://bestlearningjava.blogspot.com/2020/03/java-propertyutils-event-getting-in.html
Belum ada Komentar untuk "Java Propertyutils Event - Getting In Addition To Setting Properties Past Times Name"
Posting Komentar