The interface org.springframework.beans.BeanInfoFactory is an alternative to java.beans.Introspector to create a Spring specific enhanced implementation of java.beans.BeanInfo. BeanInfoFactory has only one method
BeanInfo getBeanInfo(Class<?> beanClass)
BeanInfoFactory implementation, org.springframework.beans.ExtendedBeanInfoFactory, accepts JavaBeans "non-standard" setter methods as 'writable' which returns some values instead of void.
This is the description from org.springframework.beans.ExtendedBeanInfo . This is a package private class which is a wrapper around standard java.beans.BeanInfo:
Wrap the given BeanInfo instance; copy all its existing property descriptors locally, wrapping each in a custom indexed or non-indexed PropertyDescriptor variant that bypasses default JDK weak/soft reference management; then search through its method descriptors to find any non-void returning write methods and update or create the corresponding PropertyDescriptor for each one found.
Spring also provides provides following methods to obtain instances of PropertyDescriptor (these instances are obtained via org.springframework.beans.ExtendedBeanInfo ):
BeanWrapper methods:
PropertyDescriptor getPropertyDescriptor(String propertyName) throws InvalidPropertyException;
PropertyDescriptor[] getPropertyDescriptors();
BeanUtil methods
public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName) throws BeansException
public static PropertyDescriptor findPropertyForMethod(Method method)
Example
Bean
package com.logicbig.example;
public class Person {
private String name;
public String getName() {
return name;
}
public String setName(String name) {
this.name = name;
return name;
}
}
Using BeanInfoFactory
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) ------
Using BeanWrapper#getPropertyDescriptors()
package com.logicbig.example;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import java.beans.PropertyDescriptor;
public class BeanWrapperPropertyDescriptorExample {
public static void main (String[] args) {
BeanWrapper bw = new BeanWrapperImpl(new Person());
printPropertyDescriptor(bw.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("------");
}
}
}
OutputName: 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) ------
Using BeanUtils#getPropertyDescriptors()
package com.logicbig.example;
import org.springframework.beans.BeanUtils;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
public class BeanUtilPropDescriptorExample {
public static void main (String[] args) throws IntrospectionException {
PropertyDescriptor[] javaDescriptor = Introspector.getBeanInfo(Person.class)
.getPropertyDescriptors();
System.out.println("-- jdk property descriptor --");
printPropertyDescriptor(javaDescriptor);
PropertyDescriptor[] springDescriptor = BeanUtils.getPropertyDescriptors(Person.class);
System.out.println("-- spring property descriptor --");
printPropertyDescriptor(springDescriptor);
}
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-- jdk property descriptor -- 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 ------ -- spring property descriptor -- 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) ------
Example ProjectDependencies and Technologies Used: - spring-context 6.1.2 (Spring Context)
Version Compatibility: 3.2.3.RELEASE - 6.1.2 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 17
- Maven 3.8.1
|