解决Caused by: org.springframework.beans.NotWritablePropertyException总结

本文解决了Spring XML中使用Setter注入属性时遇到的NotWritablePropertyException异常问题。主要原因是属性名或getter/setter方法定义不正确及XML配置文件错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       提出:今天做springxml的setter注入属性demo出现的属性无法注入的bug.

      错误信息如下:

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property '27' of bean class [com.wg.xml_setter.Person]: Bean property '27'

                                      is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

        解决思路:

                            (1):一般属性的正确的getter和setter的第二个字母是大写,有时利用快捷键生成时第二个字母是小写,故无法注入.

                                           属性编写规范:忌属性名在合成单词时都使用大写

                            (2):检查xml配置文件,我的错误就是手残写错地方导致的

                                 

 

                                       这里我把本该写age的填了值

转载于:https://www.cnblogs.com/w-gao/p/7136334.html

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'httpInvokerConfig': Unsatisfied dependency expressed through field 'appMerchantService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appMerchantDec' defined in ServletContext resource [/WEB-INF/tpaic-auto-net-servlet.xml]: Cannot resolve reference to bean 'dispatchService' while setting bean property 'dispatch'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dispatchService' defined in ServletContext resource [/WEB-INF/tpaic-auto-net-servlet.xml]: Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.tpaic.nonmotor.biz' defined in class path resource [com/tpaic/nonmotor/biz/beanRefContext.xml]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.context.support.ClassPathXmlApplicationContext]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'brandAutoTypeService' defined in class path resource [com/tpaic/nonmotor/biz/brand-service-context.xml]: Cannot resolve reference to bean 'contractBoSupport' while setting bean property 'contractBoSupport'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contractBoSupport' defined in class path resource [com/tpaic/nonmotor/biz/service-bo.xml]: Cannot resolve reference to bean 'inquireDetailService' while setting bean property 'inquireDetailService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'inquireDetailService' defined in class path resource [com/tpaic/nonmotor/biz/service-context-part.xml]: Cannot resolve reference to bean 'inquireDetailBoService' while setting bean property 'inquireDetailBoService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'inquireDetailBoService' defined in class path resource [com/tpaic/nonmotor/biz/service-bo.xml]: Cannot resolve reference to bean 'applyPolicyBo' while setting bean property 'applyPolicyBo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applyPolicyBo' defined in class path resource [com/tpaic/nonmotor/biz/service-bo.xml]: Cannot resolve reference to bean 'commonParameterSupport' while setting bean property 'common'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'commonParameterSupport' defined in class path resource [com/tpaic/nonmotor/biz/service-context-part.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'aggregatedSearchService' of bean class [com.tpaic.nonmotor.biz.util.CommonParameterSupport]: Bean property 'aggregatedSearchService' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
最新发布
06-11
### 关于 `org.springframework.beans.factory` 报错的解决方案 #### 1. **NoSuchBeanDefinitionException** 当遇到 `org.springframework.beans.factory.NoSuchBeanDefinitionException` 错误时,通常是因为 Spring 容器无法找到指定类型的 Bean 实例。这可能是由于以下原因之一: - 指定的类未被正确扫描到上下文中。 - 类上缺少必要的注解(如 `@Component`, `@Service`, 或者 `@Repository`)[^1]。 解决方法包括: - 确保目标类上有合适的组件注解,并且其包路径已被 Spring 扫描范围覆盖。 - 如果使用 XML 配置,则需确认 `<context:component-scan>` 是否包含了该类所在的包。 ```java // 示例:确保 UserDAO 被标记为可扫描的组件 package com.pri.dao; import org.springframework.stereotype.Repository; @Repository public class UserDao { // DAO 方法实现... } ``` --- #### 2. **BeanDefinitionOverrideException** 此错误表明在定义某个 Bean 时出现了重复定义的问题。可能的原因有: - 同一名称的 Bean 在不同的配置文件或配置类中多次声明。 - 使用了旧版 Spring 版本,默认不允许重写已有 Bean 的定义[^2]。 解决方式如下: - 明确区分不同配置之间的作用域,避免命名冲突。 - 若确实需要允许 Bean 重写,在较新的 Spring 版本中可以通过设置 `spring.main.allow-bean-definition-overriding=true` 来启用这一功能。 ```properties # application.properties 中添加以下内容 spring.main.allow-bean-definition-overriding=true ``` --- #### 3. **BeanCreationException** 此类异常通常是由于 Bean 初始化过程中发生的具体问题引起的,比如属性类型不匹配、依赖缺失或其他运行时错误。具体原因可以从堆栈跟踪中进一步分析[^3]。 常见场景及其修复措施: - 属性注入失败:检查对应的字段是否有正确的 setter 方法,以及是否提供了合法的数据类型。 - 外部资源加载失败:验证诸如数据库连接池、外部服务接口等是否存在初始化障碍。 示例代码展示如何处理潜在的类型转换问题: ```java // 假设 userBean 的 age 字段应为 int 类型 @Configuration public class AppConfig { @Bean public UserBean userBean() { return new UserBean(25); // 正确传递整数值而非字符串 } } class UserBean { private final int age; public UserBean(int age) { this.age = age; } public int getAge() { return age; } } ``` --- #### 4. **其他通用排查手段** 除了针对特定异常采取行动外,还有一些普遍适用的方法可以帮助定位并解决问题[^4]: - 利用日志记录详细信息,通过调整 logging level 至 DEBUG 或 TRACE 获取更多线索。 - 运行单元测试单独验证各模块的功能完整性。 - 对复杂项目结构采用可视化工具(例如 IntelliJ IDEA 提供的 Spring 插件)辅助理解整体架构布局。 --- ### 总结 以上列举了几种典型的 `org.springframework.beans.factory` 下属异常类别及其对应处置策略。实际操作中还需结合具体情况灵活应对。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值