目录
一、问题描述
二、问题分析
1、代理类
2、代理过程
三、结论
上接《Spring循环引用-@Async注解启动报错分析》
Bean1Service注解@Async
启动报错
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.1)
2021-12-19 11:16:45.271 INFO 168 --- [ main] com.lb.reference.ReferenceApplication : Starting ReferenceApplication using Java 1.8.0_191 on DESKTOP-58VVQ1F with PID 168 (D:\java\ws_1\cycle-reference\target\classes started by liangbo in D:\java\ws_1\cycle-reference)
2021-12-19 11:16:45.276 INFO 168 --- [ main] com.lb.reference.ReferenceApplication : No active profile set, falling back to default profiles: default
2021-12-19 11:16:46.850 INFO 168 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8090 (http)
2021-12-19 11:16:46.865 INFO 168 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-12-19 11:16:46.865 INFO 168 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-12-19 11:16:46.949 INFO 168 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-12-19 11:16:46.949 INFO 168 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1608 ms
2021-12-19 11:16:47.050 WARN 168 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'bean1Service': Bean with name 'bean1Service' has been injected into other beans [bean2Service] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.
2021-12-19 11:16:47.055 INFO 168 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2021-12-19 11:16:47.202 INFO 168 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-12-19 11:16:47.263 ERROR 168 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'bean1Service': Bean with name 'bean1Service' has been injected into other beans [bean2Service] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:638) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:923) ~[spring-context-5.3.2.jar:5.3.2]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:588) ~[spring-context-5.3.2.jar:5.3.2]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.1.jar:2.4.1]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) [spring-boot-2.4.1.jar:2.4.1]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.4.1.jar:2.4.1]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) [spring-boot-2.4.1.jar:2.4.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) [spring-boot-2.4.1.jar:2.4.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) [spring-boot-2.4.1.jar:2.4.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) [spring-boot-2.4.1.jar:2.4.1]
at com.lb.reference.ReferenceApplication.main(ReferenceApplication.java:13) [classes/:na]
Bean1Service注解@Async改@Transactional
启动正常
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.1)
2021-12-19 10:40:12.879 INFO 11056 --- [ main] com.lb.reference.ReferenceApplication : Starting ReferenceApplication using Java 1.8.0_191 on DESKTOP-58VVQ1F with PID 11056 (D:\java\ws_1\cycle-reference\target\classes started by liangbo in D:\java\ws_1\cycle-reference)
2021-12-19 10:40:12.883 INFO 11056 --- [ main] com.lb.reference.ReferenceApplication : No active profile set, falling back to default profiles: default
2021-12-19 10:40:14.363 INFO 11056 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8090 (http)
2021-12-19 10:40:14.376 INFO 11056 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-12-19 10:40:14.377 INFO 11056 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-12-19 10:40:14.460 INFO 11056 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-12-19 10:40:14.460 INFO 11056 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1506 ms
2021-12-19 10:40:14.728 INFO 11056 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-12-19 10:40:15.437 INFO 11056 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8090 (http) with context path ''
2021-12-19 10:40:15.460 INFO 11056 --- [ main] com.lb.reference.ReferenceApplication : Started ReferenceApplication in 3.1 seconds (JVM running for 3.785)
@Async生成代理逻辑是后置处理器放org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor.postProcessAfterInitialization(Object, String);在实例化之后就生成代理对象;
@Transactional生成代理逻辑是后置处理器方法org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator.getEarlyBeanReference(Object, String);在获取提前暴露的引用时就生成代理对象。
(1)、bean1在创建过程中,提前暴露引用工厂类加入三级缓存singletonFactories。
(2)、执行至方法org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(String, RootBeanDefinition, BeanWrapper)时,由于依赖bean2,此时bean2还没初始化,就像bean1一样进行初始化。
(3)、又由于bean2依赖bean1,初始化执行方法org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(String, RootBeanDefinition, BeanWrapper)时,进行属性bean1填充,调用获取bean1的方法org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(String),该方法从三级缓存singletonFactories工厂生成bean1实例,其中方法org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getEarlyBeanReference(String, RootBeanDefinition, Object)调用实现SmartInstantiationAwareBeanPostProcessor接口类的方法getEarlyBeanReference进行生成事务代理对象。
(4)、生成事务代理类InfrastructureAdvisorAutoProxyCreator实现了SmartInstantiationAwareBeanPostProcessor,刚好代理逻辑在getEarlyBeanReference方法中,所以bean2的属性bean1已经是代理后的对象;而生成异步代理类AsyncAnnotationBeanPostProcessor只是实现BeanPostProcessor,代理逻辑在postProcessAfterInitialization方法。
(5)、初始化完bean2后,返回接着初始化bean1,方法initalizeBean没有对bean1事务代理进行包装,故exposedObject == bean成立;而方法initalizeBean会对bean1生成异步代理对象,故exposedObject != bean。
@Async和@Transational代理都是动态代理,在循环引用时,初始化表现有差异,是代理位置不一致造成的