最近在自学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();
}
}