You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring上下文初始化异常:Bean属性'id'不可写或Setter方法无效问题排查

解决Spring的NotWritablePropertyException异常

你遇到的这个问题核心很明确:Spring容器无法给emp Bean的id属性赋值,根源是对应的Java类com.avi.spring.firstspring.employee没有提供符合Spring规范的Setter方法,或者Setter的参数类型和Getter的返回类型不匹配。

常见排查方向及解决方案:

  • 缺少规范的Setter方法:检查你的employee类,确保为id属性定义了正确的Setter。比如id是int类型的话,Setter应该写成这样:

    public void setId(int id) {
        this.id = id;
    }
    

    注意方法名必须是set开头,后面跟属性名的大写首字母(驼峰命名规则),别把setId写成setid或者其他错误格式。

  • Getter与Setter类型不匹配:比如Getter返回的是Integer(包装类),但Setter参数用了int(基本类型),虽然Spring通常能兼容,但最好保持两者类型完全一致。示例如下:

    // 正确示例:Getter和Setter类型统一
    public Integer getId() {
        return id;
    }
    
    public void setId(Integer id) {
        this.id = id;
    }
    
  • 配置文件属性名拼写错误:核对beans.xml里配置的属性名id是否和Java类中的属性名完全一致(包括大小写),Spring默认严格按驼峰规则匹配,别把id写成Id或者其他拼写错误。

你遇到的完整异常信息:

警告:上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.BeanCreationException:创建类路径资源[beans.xml]中定义的名称为'emp'的Bean时出错:设置属性值出错;嵌套异常为org.springframework.beans.NotWritablePropertyException:Bean类[com.avi.spring.firstspring.employee]的属性'id'无效:Bean属性'id'不可写或Setter方法无效。Setter的参数类型是否与Getter的返回类型匹配?
主线程中出现异常:org.springframework.beans.factory.BeanCreationException:创建类路径资源[beans.xml]中定义的名称为'emp'的Bean时出错:设置属性值出错;嵌套异常为org.springframework.beans.NotWritablePropertyException:Bean类[com.avi.spring.firstspring.employee]的属性'id'无效:Bean属性'id'不可写或Setter方法无效。Setter的参数类型是否与Getter的返回类型匹配?

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1711)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1426)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:847)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at com.avi.spring.firstspring.test.main(test.java:9)
引起原因:org.springframework.beans.NotWritablePropertyException:Bean类[com.avi.spring.firstspring.employee]的属性'id'无效:Bean属性'id'不可写或Setter方法无效。Setter的参数类型是否与Getter的返回类型匹配?
at org.springframework.beans.BeanWrapperImpl.createNotWritablePropertyException(BeanWrapperImpl.java:243)
at org.springframework.beans.AbstractNestablePropertyAccessor.processLocalProperty(AbstractNestablePropertyAccessor.java:426)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:278)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:266)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:97)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:77)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1707)
... 13 more

内容的提问来源于stack exchange,提问作者Avinash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 19:22:55