开发者社区> 问答> 正文

spring 对象引用报错??报错

使用spring注解后,引用对象就报错,
@Service("UserBiz")
public class UserBizImpl implements UserBiz{

 @Autowired
 private UserDao userDao;

 public boolean login(UserBean userBean) {
  return userDao.login(userBean);
 }
}

xml中如下配置

<bean id="userBiz" class="com.szy.biz.impl.UserBizImpl">
    <property name="userDao" ref="userDao"></property>
</bean>
<!--指定登录的实现bean  -->
<bean id="userDao" class="com.szy.dao.impl.Test"></bean>

这样配置后就报org.springframework.beans.NotWritablePropertyException: Invalid property 'userDao' of bean class [com.szy.biz.impl.UserBizImpl]: Bean property 'userDao' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

如果我将<property》去掉

 <bean id="userBiz" class="com.szy.biz.impl.UserBizImpl">
 <bean id="userDao" class="com.szy.dao.impl.Test"></bean>

运行正常,求解?

展开
收起
爱吃鱼的程序员 2020-06-22 21:10:12 372 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    不是用了@autowired就不用setter跟getter了吗,怎么会报这种错误

    <context:component-scan/>这个是有的

    引用来自“rouyh”的答案

    <context:component-scan/>其实是有加的<!--启动MVC注解驱动--><mvc:annotation-driven/><!--扫描Controller--><context:component-scanbase-package="com.szy.controller"/>出错主要信息在于userBiz中的ref引用,去掉propery引用就好了,原因是找到了,,但主要是不知道为什么重复注入,要么用xml要么用annotation你两种都用是什么情况?我想错了,事实证明有了注解不用指定其实现类,你要么保留@Autowired去掉property,要么保留property去掉Autowired,但是要加上getset。回复 @红鼠:非也,主页和xml是同样的功能,其一即可@Autowired和<propertyname="userDao"ref="userDao"></property>是重复的!用了注解也得用xml呀,这里是指定其实现类奇怪的是你不知道service实际上就已经注册了UserBizService的实现了,把这个去掉<property  name="userDao"  ref="userDao"></property>
    2020-06-22 21:10:29
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多