spring 错误(org.springframework.beans.NotWritablePropertyException)依赖注入配置

 
spring 错误(org.springframework.beans.NotWritablePropertyException)
  org.springframework.beans.NotWritablePropertyException: Invalid property 'postDao' of bean class?

  出现异常的原因是在application-xxx.xml中property name的错误。

  <property name="...."> 中name的名字是与bean的set方法相关的,而且要注意大小写。

  比如:
public class PostManageImpl extends BaseManage implements PostManage {
 private PostDAO dao = null;
 public void setPostDAO(PostDAO postDAO){
  this.dao = postDAO;
 }
}
那么xml的定义应该是:
<bean id="postManage" parent="txProxyTemplate">
<property name="target">
 <bean class="com.yz.spring.service.implement.PostManageImpl">
  <property name="postDAO"><ref bean="postDAO"/></property> 对
  <property name="dao"><ref bean="postDAO"/></property> 错
 </bean>
</property>
</bean>

 

package com.bjsxt.spring.dao;

public interface UserDao {
 
 public void save(String username, String password);
}

 

package com.bjsxt.spring.dao;

public class UserDao4MySqlImpl implements UserDao {

 public void save(String username, String password) {
  System.out.println("--------UserDao4MySqlImpl.save()-------");
 }
}

 
 

package com.bjsxt.spring.manager;

public interface UserManager {
 
 public void save(String username, String password);
}

 
//注意set方法是在用到bean的class文件中定义,而不是在对应的DAO实现中定义

package com.bjsxt.spring.manager;

import com.bjsxt.spring.dao.UserDao;

public class UserManagerImpl implements UserManager {
 
 private UserDao userDao;
 
// public UserManagerImpl(UserDao userDao) {
//  this.userDao = userDao;
// }
 
 public void save(String username, String password) {
  this.userDao.save(username, password);
 }

 public void setUserDao(UserDao userDao) {
  this.userDao = userDao;
 }
}

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
          
<!--bean标签中id的值自己随意起,不过不能让他们重名,class属性的值必须是对应的实现类,不能是接口。          -->
<!--配置DAO,mysql实现与Oracle实现-->
 <bean id="userDao4MySqlImpl" class="com.bjsxt.spring.dao.UserDao4MySqlImpl"/>
 <bean id="userDao4OracleImpl" class="com.bjsxt.spring.dao.UserDao4OracleImpl"/>
 
<!--配置manager -->
 <bean id="userManager" class="com.bjsxt.spring.manager.UserManagerImpl">
  
<!--   构造方法注入,ref属性值是对应的bean标签的id属性值 -->
<!--  <constructor-arg ref="userDao4OracleImpl"/>-->
  
<!-- setter方法注入,name属性值是UserManage.java文件中定义的对象名userDao,ref是对应的bean标签中id属性的值。  -->
   <property name="userDao" ref="userDao4MySqlImpl"/>
<!--   <property name="userDao" ref="userDao4OracleImpl"/>-->
 </bean>
 
</beans>

 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值