日期:2014-05-19  浏览次数:20693 次

用JdbcDaoSupport整合出现的问题
action: null
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sdao' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'datasource' of bean class [dao.StudentDAO]: Bean property 'datasource' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?
org.springframework.beans.NotWritablePropertyException: Invalid property 'datasource' of bean class [dao.StudentDAO]: Bean property 'datasource' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?

Servlet /SpringHibenate1 threw load() exception



我在写public class StudentDAO extends JdbcDaoSupport implements IStudentDAO{
JdbcTemplate jt=this.getJdbcTemplate();

就出现这问题了,配置那里没有弄错的,搜了好久了

------解决方案--------------------
把你的配置、代码提出来

好像说的是set方法不对
------解决方案--------------------
很明显啊,sdao里没配置datasouse属性。
LZ应该:在文件[/WEB-INF/applicationContext.xml]的sdao(<bean>)注入datasourse

引用楼主 lyxwwwlyx 的帖子:
action: null
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sdao' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'datasource' of bean class [dao.StudentDAO]: Bean property 'datasource' is not writable or has …

------解决方案--------------------
解释一下:
继承了JdbcDaoSupport类的作用是这个类本身有JdbcTemplate 及datasourse的get/set方法,但是JdbcDaoSupport并不没有提供这两个属性的实例化,因此,还是要在配置问题中注入这个两个属性。
------解决方案--------------------
看了你的配置文件了...感觉怪怪的...

第一点:
你的StudentDAO继承的是JdbcDaoSupport类,但是JdbcDaoSupport类只有两个属性dataSource和jdbcTemplate,所以应该要注入的是dataSource和jdbcTemplate两个bean,而不是session。而这样的话,你的项目就根本用不到hibernante的。

第二点:
若要使用Hibernate的话,StudentDAO应该继承HibernateDaoSupport类,然后注入sessionFactory或HibernateTemplate,使用HibernateTemplate模板进行数据的操作。