由以下原因引起:org.springframework.beans.NotWritablePropertyException:bean 类 [com.uz.SysConfig] 的无效属性“isTestCtx”:Bean 属性“isTestCtx”不可写或具有无效的 setter 方法。 setter 的参数类型与 getter 的返回类型是否匹配?
型号代码:
public class SysConfig {
@Getter
@Setter
@Value("${isTestCtx}")
private boolean isTestCtx;
@PostConstruct
public void init(){
log.info(" isTestCtx: {}", isTestCtx);
}
}
使用lombok生成的代码,我可以看到
public boolean isTestCtx() {
return this.isTestCtx;
}
public void setTestCtx(boolean isTestCtx) {
this.isTestCtx = isTestCtx;
}
一切顺利。我不知道为什么会出现这个错误。 有人可以帮忙吗?
最佳答案
Lombok 生成的代码似乎是错误的(显然它在 page 底部的细则中,尽管它只提到了 getter)。
对于 boolean
属性 isTextCtx
,根据 JavaBeans 规范,预期的 getter 和 setter 必须是:
public boolean isIsTestCtx() {
return this.isTestCtx;
}
public void setIsTestCtx(boolean isTestCtx) {
this.isTestCtx = isTestCtx;
}
将您的属性重命名为 testCtx
应该可以解决问题。
@Getter
@Setter
@Value("${isTestCtx}")
private boolean testCtx;
关于java - Spring中的NotWritablePropertyException错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39737637/