Close

Spring Framework - ExtendedBeanInfoFactory Examples

Spring Framework 

ExtendedBeanInfoFactory is BeanInfoFactory implementation which provides additional flexibilities on returned BeanInfo as compare to java.beans.Introspector#getBeanInfo. Following example shows that a setter with a returned value is also considered a readable property.

package com.logicbig.example.beaninfofactory;

import org.springframework.beans.ExtendedBeanInfoFactory;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

public class BeanInfoFactoryExample {
public static void main (String[] args) throws IntrospectionException {
System.out.println("-------- using JDK Introspector -");
useIntrospector();
System.out.println("-------- using Spring BeanInfoFactory -");
useBeanInfoFactory();
}

private static void useBeanInfoFactory () throws IntrospectionException {
ExtendedBeanInfoFactory factory = new ExtendedBeanInfoFactory();
BeanInfo beanInfo = factory.getBeanInfo(MyBean.class);
for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
System.out.println(propertyDescriptor);
}
}

private static void useIntrospector () throws IntrospectionException {
BeanInfo beanInfo = Introspector.getBeanInfo(MyBean.class);
for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
System.out.println(propertyDescriptor);
}
}

private static class MyBean {
private String name;

public String getName () {
return name;
}

public String setName (String str) {
this.name = "MyBean-" + str;
return name;
}
}
}

Output

-------- using JDK Introspector -
java.beans.PropertyDescriptor[name=class; propertyType=class java.lang.Class; readMethod=public final native java.lang.Class java.lang.Object.getClass()]
java.beans.PropertyDescriptor[name=name; propertyType=class java.lang.String; readMethod=public java.lang.String com.logicbig.example.beaninfofactory.BeanInfoFactoryExample$MyBean.getName()]
-------- using Spring BeanInfoFactory -
SimplePropertyDescriptor[name=class, propertyType=class java.lang.Class, readMethod=public final native java.lang.Class java.lang.Object.getClass(), writeMethod=null]
SimplePropertyDescriptor[name=name, propertyType=class java.lang.String, readMethod=public java.lang.String com.logicbig.example.beaninfofactory.BeanInfoFactoryExample$MyBean.getName(), writeMethod=public java.lang.String com.logicbig.example.beaninfofactory.BeanInfoFactoryExample$MyBean.setName(java.lang.String)]




ExtendedBeanInfoFactory is BeanInfoFactory implementation which provides additional flexibilities on returned BeanInfo as compare to java.beans.Introspector#getBeanInfo. Following example shows that a setter with a returned value is also considered a readable property.

package com.logicbig.example;

import org.springframework.beans.BeanInfoFactory;
import org.springframework.beans.ExtendedBeanInfoFactory;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

public class BeanInfoFactoryExample {
public static void main (String[] args) throws IntrospectionException {
System.out.println("-------- using JDK Introspector --");
BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
printPropertyDescriptor(beanInfo.getPropertyDescriptors());

System.out.println("-------- using Spring BeanInfoFactory --");
BeanInfoFactory factory = new ExtendedBeanInfoFactory();
BeanInfo springBeanInfo = factory.getBeanInfo(Person.class);
printPropertyDescriptor(springBeanInfo.getPropertyDescriptors());
}

private static void printPropertyDescriptor(PropertyDescriptor[] descriptors) {
for (PropertyDescriptor descriptor : descriptors) {
System.out.println("Name: "+descriptor.getName());
System.out.println("Reader: "+descriptor.getReadMethod());
System.out.println("Writer: "+descriptor.getWriteMethod());
System.out.println("------");
}
}
}

Output

-------- using JDK Introspector --
Name: class
Reader: public final native java.lang.Class java.lang.Object.getClass()
Writer: null
------
Name: name
Reader: public java.lang.String com.logicbig.example.Person.getName()
Writer: null
------
-------- using Spring BeanInfoFactory --
Name: class
Reader: public final native java.lang.Class java.lang.Object.getClass()
Writer: null
------
Name: name
Reader: public java.lang.String com.logicbig.example.Person.getName()
Writer: public java.lang.String com.logicbig.example.Person.setName(java.lang.String)
------
Original Post




See Also