Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 4 cannot execute Java 8 default methods

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.

like image 823
Adam Szecowka Avatar asked May 27 '15 08:05

Adam Szecowka


People also ask

What is default method Java 8?

The default methods were introduced to provide backward compatibility so that existing interfaces can use the lambda expressions without implementing the methods in the implementation class. Default methods are also known as defender methods or virtual extension methods.

Can we override default method in Java 8?

A default method cannot override a method from java.

What is default and static methods in interface in Java 8?

Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces. A static method is a method that is associated with the class in which it is defined rather than with any object.

Can we have default method in functional interface?

A functional interface can have any number of default methods.


1 Answers

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)

like image 102
dunni Avatar answered Oct 18 '22 17:10

dunni