Spring错误之org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'bookService' is expected to be of type 'pw.fengya.tx.BookService' but was actually of type 'com.sun.proxy.$Proxy1
1 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cashier': Unsatisfied dependency expressed through field 'bookService'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'bookService' is expected to be of type 'pw.fengya.tx.BookService' but was actually of type 'com.sun.proxy.$Proxy19' 2 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587) 3 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) 4 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373) 5 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1344) 6 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) 7 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) 8 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) 9 at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$176/667821226.getObject(Unknown Source) 10 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 11 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) 12 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) 13 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) 14 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) 15 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) 16 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144) 17 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85) 18 at pw.fengya.tx.Test.<init>(Test.java:19) 19 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 20 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 21 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 22 at java.lang.reflect.Constructor.newInstance(Constructor.java:408) 23 at org.junit.platform.commons.util.ReflectionUtils.newInstance(ReflectionUtils.java:347) 24 at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:60) 25 at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.instantiateTestClass(ClassTestDescriptor.java:197) 26 at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.instantiateAndPostProcessTestInstance(ClassTestDescriptor.java:188) 27 at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$testInstanceProvider$1(ClassTestDescriptor.java:181) 28 at org.junit.jupiter.engine.descriptor.ClassTestDescriptor$$Lambda$154/1860944798.getTestInstance(Unknown Source) 29 at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:80) 30 at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:57) 31 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:60) 32 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null$2(HierarchicalTestExecutor.java:92) 33 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$136/201556483.accept(Unknown Source) 34 at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) 35 at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) 36 at java.util.Iterator.forEachRemaining(Iterator.java:116) 37 at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) 38 at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512) 39 at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502) 40 at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) 41 at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) 42 at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) 43 at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418) 44 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:92) 45 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$133/1287712235.execute(Unknown Source) 46 at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66) 47 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77) 48 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null$2(HierarchicalTestExecutor.java:92) 49 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$136/201556483.accept(Unknown Source) 50 at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) 51 at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) 52 at java.util.Iterator.forEachRemaining(Iterator.java:116) 53 at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) 54 at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512) 55 at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502) 56 at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) 57 at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) 58 at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) 59 at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418) 60 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:92) 61 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$133/1287712235.execute(Unknown Source) 62 at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66) 63 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77) 64 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:51) 65 at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43) 66 at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170) 67 at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154) 68 at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90) 69 at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:86) 70 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 71 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) 72 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) 73 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) 74 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) 75 Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'bookService' is expected to be of type 'pw.fengya.tx.BookService' but was actually of type 'com.sun.proxy.$Proxy19' 76 at org.springframework.beans.factory.support.DefaultListableBeanFactory.checkBeanNotOfRequiredType(DefaultListableBeanFactory.java:1527) 77 at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1505) 78 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) 79 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) 80 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584) 81 ... 72 more
最近在学习Spring框架的时候,很容易犯下的一个错误,需要在配置文件中打开aop命名空间,并设置如下属性:
<aop:config proxy-target-class="true"></aop:config>
不知道为什么,在使用注解配置bean的时候,很容易报的一个错,尤其是自定义的接口与实现类