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)]
Original Post




See Also