8

I have defined interface

public interface MyInterface {
  default void setOrder(int a){ }
  default int getOrder(){return 123;}
}

and implementation

public class MyInterfaceImpl implements MyInterface {}

In my spring configuration file I have defined following bean:

    <bean id="a" class="my.package.MyInterfaceImpl">
    <property name="order" value="999"/>
</bean>

When I create spring context I got following error:

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'order' of bean class [my.package.MyInterfaceImpl]: Bean property 'order' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

I am using spring in version 4.1.6.RELEASE. So my question is why it is not possible to execute method setOrder which is default method from interface MyInterface? It seems that spring completely ignore such methods.

Adam Szecowka
  • 675
  • 3
  • 8
  • 20
  • I can only make a guess, but maybe spring checks the methods by setting a value and then getting it back assuming the getter returns the previously set value. In your example this would not succeed. Try to assign to a field or override them to test this. – Tobias Kloss May 27 '15 at 08:48
  • 1
    Please try to change `` to `` as your default getter is returning `123`not `999`. – Tobias Kloss May 27 '15 at 08:57

2 Answers2

6

Handling of default methods in interfaces will come with Spring 4.2, so until then either use the release candidates or milestones or don't use default methods with Spring (https://jira.spring.io/browse/SPR-12822 or https://jira.spring.io/browse/SPR-10919)

dunni
  • 43,386
  • 10
  • 104
  • 99
1

This issue is still present in the Spring 4.2.5.RELEASE

I have thrown together an example that showcases it on Github here: https://github.com/cjbooms/spring-default-methods

And logged a ticket with Spring here: https://jira.spring.io/browse/SPR-14198

cjbooms
  • 163
  • 8