Spring XML + BeanInfos

Why can’t Spring XML be used to set property javax.swing.cursor?

<bean id="foo" class="javax.swing.JPanel">
  <property name="cursor">
    <bean class="com.adamish.foo.CursorFactory"
          factory-method="createCursor" />

The resultant exception:
org.springframework.beans.NotWritablePropertyException: Invalid property ‘cursor’ of bean class [javax.swing.JPanel]: Bean property ‘cursor’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

Cursor has a public getCursor and setCursor(), so what’s the problem? Well it turns out spring XML builder doesn’t use simple java reflection as you might expect… Under-the-hood it uses java.beans.Introspector, this will create a default java.beans.BeanInfo instance in most cases listing all attributes with public getters/setters. If however, a design-time BeanInfo is available then this will be used instead, such classes allow attributes to filtered and custom editors assigned.

So where’s the BeanInfo for JPanel? There isn’t one as such but there is one for super-class java.awt.Component – sun.beans.info.ComponentBeanInfo. This BeanInfo only defines {name, background, foreground, font, enabled, visible and focusable}, but not cursor.

Leave a comment

Your email address will not be published.