iteye_20732 2009-01-04 18:37
浏览 339
已采纳

Spring和Ibatis整合时出错了

最近在自学ibatis,在网上搜索了一些视频资料,今天准备来整一个Spring+ibatis,但是这个问题现在把我难住了。报错信息如下:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'empservice' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'sqlMapClientTemplate' of bean class [com.inventec.service.EmployeeService]: Bean property 'sqlMapClientTemplate' 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 'sqlMapClientTemplate' of bean class [com.inventec.service.EmployeeService]: Bean property 'sqlMapClientTemplate' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:670)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:572)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:737)
at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:764)
at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:753)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1057)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:857)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:378)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:233)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:145)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:283)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:313)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:87)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:72)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:63)
at com.inventec.service.Test.main(Test.java:10)

我的Spring的ApplicationContext.xml 是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<!--此bean告诉Spring去哪找数据库的配置信息,因为有此Bean才出现下面用${}标记来取变量的语句-->
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

jdbc.properties

<!--配置一个数据源,根据上面propertyConfig指定的location去找数据库连接的配置信息-->
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
        <value>${jdbc.driver}</value>
    </property>
    <property name="url">
        <value>${jdbc.url}</value>
    </property>
    <property name="username">
        <value>${jdbc.username}</value>
    </property>
    <property name="password">
        <value></value>
    </property>
</bean>

<!--根据dataSource和configLocation创建一个SqlMapClient-->
<bean id="sqlMapClient"
    class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation">
        <value>SqlMapConfig.xml</value>
    </property>
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
</bean>

<!--根据sqlMapClien创建一个SqlMapClient模版类-->
<bean id="sqlMapClientTemplate"
    class="org.springframework.orm.ibatis.SqlMapClientTemplate">
    <property name="sqlMapClient">
        <ref bean="sqlMapClient" />
    </property>
</bean>

<!--将上面的模版类织入到我们的DAO对象中-->
<bean id="empservice" class="com.inventec.service.EmployeeService">
    <property name="sqlMapClientTemplate">
        <ref bean="sqlMapClientTemplate" />
    </property>
</bean>

EmployeeService.java是这样的:
package com.inventec.service;

import java.util.List;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import com.inventec.bean.Account;
import com.inventec.bean.Employee;

public class EmployeeService {
private SqlMapClientTemplate sqlMap;

public SqlMapClientTemplate getSqlMap() {
    return sqlMap;
}

public void setSqlMap(SqlMapClientTemplate sqlMap) {
    this.sqlMap = sqlMap;
}

public Employee getOne() {
    Employee emp = null;

        emp = (Employee) sqlMap.queryForObject("getOne", 1);
        System.out.println(emp.getEmpName() + " " + emp.getEmail());

    return emp;

}

public List getSomeData() {
    List list = null;
        list = sqlMap.queryForList("getAll", 1);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    return list;

}

public Account getOneAccount() {
    Account acc = null;
        acc = (Account) sqlMap.queryForObject("getOneAcc", 2);
        System.out.println(acc.getUserName()+" pwd:"+acc.getPassWord());

    return acc;
}

}

确定不是sqlMapconfig.xml和其他资源文件的问题,各位帮帮忙啦!小妹先谢了!!!
[b]问题补充:[/b]
在Test.java中是这样的来执行的:
public class Test extends EmployeeService {

public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext(
            "applicationContext.xml");
    EmployeeService service = (EmployeeService) context
            .getBean("empservice");
    service.getOne();

}

}

  • 写回答

2条回答 默认 最新

  • csluoyl 2009-01-04 23:24
    关注





    上面的配置有问题,按照这样的写法,EmployeeService这个类里应该有"sqlMapClientTemplate”这个属性,但我看了EmployeeService.java,没找到,这当然会报错,应该改成以下的写法:





    ,也就是把改成

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示