Spring 4无法执行Java 8默认方法。

8

我定义了一个接口

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

实施和实现
public class MyInterfaceImpl implements MyInterface {}

在我的Spring配置文件中,我定义了以下bean:
    <bean id="a" class="my.package.MyInterfaceImpl">
    <property name="order" value="999"/>
</bean>

当我创建Spring上下文时,出现以下错误:

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?

我正在使用版本为4.1.6.RELEASE的Spring。所以我的问题是为什么无法执行接口MyInterface中默认方法setOrder?似乎Spring完全忽略了这些方法。

我只能猜测,但也许Spring通过设置一个值并获取它来检查方法,假设getter返回先前设置的值。在您的示例中,这将不会成功。 尝试分配给字段或覆盖它们以进行测试。 - Tobias Kloss
1
请尝试将 <property name="order" value="999"/> 更改为 <property name="order" value="123"/>,因为您的默认 getter 返回的是 123 而不是 999 - Tobias Kloss
2个回答

6

我刚刚升级到4.2.5.RELEASE,但仍然遇到了这个问题。 - cjbooms

1

这个问题已经被修复,并计划在2016年6月8日发布的Spring 4.3版本中包含。 - cjbooms

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接