Java Propertyutils Event - Getting As Well As Setting Properties Past Times Name

PropertyUtils degree of Apache commons beanutils library is real useful together with provides yous might to modify properties of Java object at runtime. PropertyUtils enables yous to write highly configurable code where yous tin render mention of edible bean properties together with at that topographic point values from configuration rather than coded inwards Java program together with Apache PropertyUtils tin laid those properties on Java object at runtime. One pop event of how powerful PropertyUtils tin endure is display tag which provides rich tabular display for JSP pages, it uses PropertyUtils degree to instruct values of object at runtime together with than display it. You tin setup properties equally column together with solely selected columns volition endure displayed. Even larger spider web framework similar Struts together with Spring likewise uses Apache green beanutils library for getting together with setting coffee properties yesteryear name.
PropertyUtils is based on Java reflection simply provides a convenient method to operate on Java object at runtime. By using PropertyUtils yous tin instruct a map of all Object properties which enables yous to alter them at runtime yesteryear values coming from all the places similar web request, database or configuration files. In this Java tutorial nosotros volition event of how to instruct together with laid properties of coffee object using PropertyUtils degree at runtime.

This article is inwards continuation of my before post service on opened upwards origin library similar How to confine divulge of user session inwards spider web application using Spring Security together with How to perform LDAP authentication on windows Active directory using Spring Security. If yous haven’t read them already yous may uncovering them useful together with interesting.



Java Program to instruct object properties at runtime

 library is real useful together with provides yous might to  Java PropertyUtils Example - getting together with setting properties yesteryear nameHere is a unproblematic Java plan which shows how to purpose PropertyUtils degree to alter object properties or to instruct Object together with its properties at runtime. Remember nosotros don’t know anything nearly Object at compile time. On runtime Object is provided to plan along alongside mention of belongings to recollect or modify:

import org.apache.commons.beanutils.PropertyUtils;

public class PropertyUtilsTest {

    public static void main(String args[]) {

        try{
        MobilePhone flexiColor = new MobilePhone();
        //here color together with bluish strings tin come upwards from multifariousness or sources
        //e.g. configuration files, database, whatever upstream organisation 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 green PropertyUtils:

PropertyUtils provides lot of flexibility piece writing plan though that comes alongside the cost of reflection; hither are unopen to of the of import benefits of using Apache green PropertyUtils I tin intend of:

1) You tin instruct whatever belongings value at runtime without having coded for it e.g. bean.getOrder() tin endure replaced by
PropertyUtils.getProperty(bean, order) where edible bean together with lodge are runtime value together with tin change.

2) Similarly, yous tin laid whatever object belongings at runtime e.g. bean.setOrder(order) tin endure replaced by

PropertyUtils.setProperty(bean, order, new Order())

3) Apache beanUtils enables yous to write highly configurable code.

Errors together with Exception piece using PropertyUtils

If yous endeavor to laid a belongings together with getting "Exception inwards thread "main" java.lang.NoSuchMethodException: Property 'color' has no setter method" similar below:

Exception inwards 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)


There could endure dissimilar reasons for this exceptions e.g.

1) Bean degree whose belongings yous are setting is non public. Yes, yous instruct this Exception fifty-fifty if the degree is accessible similar Inner degree simply non populace 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 likewise instruct "java.lang.IllegalArgumentException: declaration type mismatch" similar below if yous 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 together with yous 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 purpose PropertyUtils class, yous require to include commons-beanutils.jar inwards your Java Classpath. together with since commons-beanutils.jar likewise has a dependency on commons-logging.jar yous require to include that equally well. If yous are using Maven for edifice your projection or maintaining dependency yous tin include the next dependency.

Maven dependency for apache green beanutils

<dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.8.2</version>
</dependency>



That’s all on How to purpose PropertyUtils from Apache commons-beanutils.jar to instruct together with laid object properties at runtime.  Apart from PropertyUtils beanutils likewise contains several other utility for dealing alongside beans inwards Java application.

Further Learning
Spring Framework 5: Beginner to Guru
How to defined Error page inwards JSP


Reference
http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/PropertyUtils.html

Komentar

Postingan populer dari blog ini

Difference Betwixt Struts Validatorform Vs Validatoractionform - Interview Question

How To Convert Inputstream To Byte Array Inwards Coffee - Two Examples

Difference Betwixt Fileinputstream Together With Filereader Inwards Coffee | Inputstream Vs Reader