Spring的Autowired注解
报错代码:
D:\pro\java\jdkaz\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\pro\IDEA\IntelliJ IDEA 2021.1\lib\idea_rt.jar=52362:D:\pro\IDEA\IntelliJ IDEA 2021.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\pro\IDEA\IntelliJ IDEA 2021.1\lib\idea_rt.jar;D:\pro\IDEA\IntelliJ IDEA 2021.1\plugins\junit\lib\junit5-rt.jar;D:\pro\IDEA\IntelliJ IDEA 2021.1\plugins\junit\lib\junit-rt.jar;D:\pro\java\jdkaz\jre\lib\charsets.jar;D:\pro\java\jdkaz\jre\lib\deploy.jar;D:\pro\java\jdkaz\jre\lib\ext\access-bridge-64.jar;D:\pro\java\jdkaz\jre\lib\ext\cldrdata.jar;D:\pro\java\jdkaz\jre\lib\ext\dnsns.jar;D:\pro\java\jdkaz\jre\lib\ext\jaccess.jar;D:\pro\java\jdkaz\jre\lib\ext\jfxrt.jar;D:\pro\java\jdkaz\jre\lib\ext\localedata.jar;D:\pro\java\jdkaz\jre\lib\ext\nashorn.jar;D:\pro\java\jdkaz\jre\lib\ext\sunec.jar;D:\pro\java\jdkaz\jre\lib\ext\sunjce_provider.jar;D:\pro\java\jdkaz\jre\lib\ext\sunmscapi.jar;D:\pro\java\jdkaz\jre\lib\ext\sunpkcs11.jar;D:\pro\java\jdkaz\jre\lib\ext\zipfs.jar;D:\pro\java\jdkaz\jre\lib\javaws.jar;D:\pro\java\jdkaz\jre\lib\jce.jar;D:\pro\java\jdkaz\jre\lib\jfr.jar;D:\pro\java\jdkaz\jre\lib\jfxswt.jar;D:\pro\java\jdkaz\jre\lib\jsse.jar;D:\pro\java\jdkaz\jre\lib\management-agent.jar;D:\pro\java\jdkaz\jre\lib\plugin.jar;D:\pro\java\jdkaz\jre\lib\resources.jar;D:\pro\java\jdkaz\jre\lib\rt.jar;E:\pro\IDEAJavaProject\ssm\ssm_Test\out\production\ssm_springTransaction;E:\pro\IDEAJavaProject\ssm\ssm_Test\ssm_springTransaction\lib\commons-logging-1.1.3.jar;E:\pro\IDEAJavaProject\ssm\ssm_Test\ssm_springTransaction\lib\druid-1.1.9.jar;E:\pro\IDEAJavaProject\ssm\ssm_Test\ssm_springTransaction\lib\mysql-connector-java-5.1.7-bin.jar;E:\pro\IDEAJavaProject\ssm\ssm_Test\ssm_springTransaction\lib\spring-aop-5.2.5.RELEASE.jar;E:\pro\IDEAJavaProject\ssm\ssm_Test\ssm_springTransaction\lib\spring-beans-5.2.5.RELEASE.jar;E:\pro\IDEAJavaProject\ssm\ssm_Test\ssm_springTransaction\lib\spring-context-5.2.5.RELEASE.jar;E:\pro\IDEAJavaProject\ssm\ssm_Test\ssm_springTransaction\lib\spring-core-5.2.5.RELEASE.jar;E:\pro\IDEAJavaProject\ssm\ssm_Test\ssm_springTransaction\lib\spring-expression-5.2.5.RELEASE.jar;E:\pro\IDEAJavaProject\ssm\ssm_Test\ssm_springTransaction\lib\spring-jdbc-5.2.5.RELEASE.jar;E:\pro\IDEAJavaProject\ssm\ssm_Test\ssm_springTransaction\lib\spring-orm-5.2.5.RELEASE.jar;E:\pro\IDEAJavaProject\ssm\ssm_Test\ssm_springTransaction\lib\spring-tx-5.2.5.RELEASE.jar;E:\pro\IDEAJavaProject\ssm\ssm_Test\ssm_springTransaction\lib\org.hamcrest.core_1.3.0.v201303031735.jar;E:\pro\IDEAJavaProject\ssm\ssm_Test\ssm_springTransaction\lib\junit.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 ssm.example.test.Test_Spring_Declarative_Transaction,testTransactional_Propagation
十二月 01, 2021 4:06:41 下午 org.springframework.context.support.ClassPathXmlApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cashierImpl': Unsatisfied dependency expressed through field 'purchaseImpl'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'purchaseImpl' is expected to be of type 'ssm.example.service.impl.PurchaseImpl' but was actually of type 'com.sun.proxy.$Proxy17'
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cashierImpl': Unsatisfied dependency expressed through field 'purchaseImpl'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'purchaseImpl' is expected to be of type 'ssm.example.service.impl.PurchaseImpl' but was actually of type 'com.sun.proxy.$Proxy17'
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at ssm.example.test.Test_Spring_Declarative_Transaction.<init>(Test_Spring_Declarative_Transaction.java:18)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'purchaseImpl' is expected to be of type 'ssm.example.service.impl.PurchaseImpl' but was actually of type 'com.sun.proxy.$Proxy17'
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1300)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1210)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
... 36 more
Process finished with exit code -1
分析:
从第5行报错原因中可以看到创建cashierImpl失败(通过purchaseImple创建失败),嵌套的原因:期望一个类型是PurchaseImpl的,但是给了一个动态代理创建的
也就是说,purchaseImpl的类型错了,查看源码发现原来的 purchaseImpl 的类型是 PurchaseImpl ,那么创建的时候肯定会出现动态代理的错误(最后的动态代理创建出来的对象要和接口一样,而不是实现类)
解决:
purchaseImpl 的类型改为 Purchase