spring 写junit测试时使用aop报错

当我在我的配置文件中加上如下配置会包下面的错误,我不加时就不会出错这是为什么

 <aop:config>
        <aop:pointcut id="interceptorPointCuts" expression="execution(* cn.qtt.service.*.*(..)) " />

        <aop:pointcut id="interceptorPointCutsNew" expression="execution(* cn.qtt.advert.service.*.*(..)) " />

        <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />

        <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCutsNew" />
    </aop:config>
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'AdvertServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.qtt.service.impl.UserServiceImpl AdvertServiceTest.userService; nested exception is java.lang.IllegalArgumentException: Can not set cn.qtt.service.impl.UserServiceImpl field AdvertServiceTest.userService to com.sun.proxy.$Proxy17
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:374) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110) ~[org.springframework.test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) ~[org.springframework.test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321) ~[org.springframework.test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) [org.springframework.test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288) [org.springframework.test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) [junit.jar:na]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290) [org.springframework.test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) [org.springframework.test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) [junit.jar:na]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) [junit.jar:na]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) [junit.jar:na]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) [junit.jar:na]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) [junit.jar:na]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) [junit.jar:na]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [org.springframework.test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) [org.springframework.test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236) [junit.jar:na]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) [org.springframework.test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) [.cp/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.qtt.service.impl.UserServiceImpl AdvertServiceTest.userService; nested exception is java.lang.IllegalArgumentException: Can not set cn.qtt.service.impl.UserServiceImpl field AdvertServiceTest.userService to com.sun.proxy.$Proxy17
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    ... 26 common frames omitted
Caused by: java.lang.IllegalArgumentException: Can not set cn.qtt.service.impl.UserServiceImpl field AdvertServiceTest.userService to com.sun.proxy.$Proxy17
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source) ~[na:1.7.0_51]
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source) ~[na:1.7.0_51]
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source) ~[na:1.7.0_51]
    at java.lang.reflect.Field.set(Unknown Source) ~[na:1.7.0_51]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    ... 28 common frames omitted

2个回答

spring的AOP实现可以使用JDK的动态代理目标对象实现是接口,而你的Can not set cn.qtt.service.impl.UserServiceImpl field AdvertServiceTest.userService to com.sun.proxy.$Proxy17这里说的数实现类,改成接口应该就可以了,或者改成CGLIB实现AOP使用不实用接口, 添加CGLIB库,SPRING_HOME/cglib/*.jar * 在spring配置文件中加入

有两个txAdvice重名了吧!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问