[quote][/quote]开发环境:eclipse 3.2
框架 spring + ibatis + dwr
单元测试时候提示的错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MsuserService' defined in file [D:\workspace\MS2\src\org\ag\ms\test\WEB-INF\applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy0] to required type [org.ag.ms.dao.impl.MsuserDaoImpl] for property 'msuserDao'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy0] to required type [org.ag.ms.dao.impl.MsuserDaoImpl] for property 'msuserDao': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1247)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:986)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:238)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:167)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:235)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:167)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:385)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:730)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:369)
at org.springframework.context.support.FileSystemXmlApplicationContext.(FileSystemXmlApplicationContext.java:93)
at org.springframework.context.support.FileSystemXmlApplicationContext.(FileSystemXmlApplicationContext.java:77)
at org.springframework.context.support.FileSystemXmlApplicationContext.(FileSystemXmlApplicationContext.java:67)
at org.ag.ms.test.AgTest.setUp(AgTest.java:14)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy0] to required type [org.ag.ms.dao.impl.MsuserDaoImpl] for property 'msuserDao'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy0] to required type [org.ag.ms.dao.impl.MsuserDaoImpl] for property 'msuserDao': no matching editors or conversion strategy found
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:104)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:59)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1244)
... 26 more
首先:spring的applicationContext.xml的内容如下:
src/org/ag/ms/test/WEB-INF/datasourceConfig.properties
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
destroy-method="close" lazy-init="default" autowire="default"
dependency-check="default">
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:org/ag/ms/xml/sqlMapConfig.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Impl</value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor"
ref="transactionInterceptor" />
</bean>
[color=red]<bean id="MsuserDaoImpl" class="org.ag.ms.dao.impl.MsuserDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="MsuserService" class="org.ag.ms.service.impl.MsuserImpl">
<property name="msuserDao" ref="MsuserDaoImpl"></property>
</bean>[/color]
单元测试:
import java.util.List;
import junit.framework.TestCase;
import org.ag.ms.dao.MsuserDao;
import org.ag.ms.model.TMsuser;
public class AgTest extends TestCase {
private org.springframework.context.ApplicationContext ac;
public void setUp(){
String applcationFile = "src/org/ag/ms/test/WEB-INF/applicationContext.xml";
ac = new org.springframework.context.support.FileSystemXmlApplicationContext(applcationFile);
}
public void testSpringBean(){
MsuserDao mui = (MsuserDao)ac.getBean("MsuserDaoImpl");
TMsuser tm = (TMsuser)mui.selectMsuserById(1);
List list = (List) mui.selectMsuserByCell("13000000004");
List list2 = (List) mui.selectMsuserByNickname("huanyi4");
System.out.println(list.size());
System.out.println(list2.size());
System.out.println(tm.getClass());
System.out.println(tm.getDestination());
}
}
没有写入:
[color=red]的时候的单元测试时通过的,加了这个bean就错误[/color]
对应的类:
package org.ag.ms.service.impl;
import java.util.List;
import org.ag.ms.dao.impl.MsuserDaoImpl;
import org.ag.ms.service.inter.MsuserInter;
public class MsuserImpl implements MsuserInter {
private MsuserDaoImpl [color=red]msuserDao[/color];
public boolean deleteMsuserById(Integer id) {
// TODO Auto-generated method stub
return false;
}
. ......
public void setMsuserDao(MsuserDaoImpl msuserDao){
this.msuserDao = msuserDao;
}
public MsuserDaoImpl getMsuserDao(){
return msuserDao;
}
}
[b]问题补充:[/b]
万分感谢layer555高手的指点,果然在spring里头需要用接口而不是用实现类。可以在dwr的测试页面中看到了该对象,而且还有对应的方法,但是又遇到新问题,就是查询出来的结果是null,测试的报错是:
[color=red]java.lang.NullPointerException[/color]
at org.ag.ms.test.AgTest.testSpringBean(AgTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)