Spring, Fluent APIs, and BeanInfo
Fluent APIs use setters that return objects instead of void. Normally, Spring won’t be able to use classes that implement a fluent API because the setter method won’t be found.
However, I have learned that it is possible to write a BeanInfo class which helps the Java reflection system. Here is a simple example:
package com.codebits;
public class PersonBeanInfo extends SimpleBeanInfo {
public BeanDescriptor getBeanDescriptor() {
return new BeanDescriptor(beanClass);
}
private final static Class beanClass = Person.class;
public PropertyDescriptor[] getPropertyDescriptors() {
try {
PropertyDescriptor name =
new PropertyDescriptor("name", beanClass);
PropertyDescriptor rv[] = { name };
return rv;
} catch (IntrospectionException e) {
throw new Error(e.toString());
}
}
}
which provides guildance for this bean:
package com.codebits;
public class Person {
private String name = null;
public String getName() { return this.name; }
public Person setName(String _name) {
this.name = _name;
return this;
}
public Person() { super(); }
}