Spring 4 cannot execute Java 8 default methods

坚强是说给别人听的谎言 提交于 2020-01-02 02:45:07

问题


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.


回答1:


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)




回答2:


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



来源:https://stackoverflow.com/questions/30477367/spring-4-cannot-execute-java-8-default-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!