2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018

04/12/2006: 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(); }
}


subscribe via RSS