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.

2
  • 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. Commented May 27, 2015 at 8:48
  • 1
    Please try to change <property name="order" value="999"/> to <property name="order" value="123"/> as your default getter is returning 123not 999. Commented May 27, 2015 at 8:57

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

1 Comment

I just upgraded to 4.2.5.RELEASE and am still encountering this issue
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

1 Comment

This has been fixed and is scheduled to be included in the Spring 4.3 release on June 8th 2016

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.