Spring Boot Startup Failure: @Autowired vs @DubboReference Bean Injection Conflict

This article analyzes a Spring Boot startup failure caused by a conflict between @Autowired and @DubboReference annotations when injecting the same service bean, explaining the underlying mechanism and providing solutions.

政采云技术
政采云技术
政采云技术
Spring Boot Startup Failure: @Autowired vs @DubboReference Bean Injection Conflict

This article analyzes a Spring Boot startup failure caused by a conflict between @Autowired and @DubboReference annotations when injecting the same service bean. The problem occurs when a Spring bean (MvcInterceptorAutoConfiguration) requires an AccessContextService bean that is defined using @DubboReference in another class (ProxyCenter).

The core issue stems from the different injection mechanisms: @DubboReference uses a BeanPostProcessor (ReferenceAnnotationBeanPostProcessor) to create proxy objects and register them as singletons during property injection, while @Autowired attempts to resolve dependencies during bean creation. When @Autowired tries to inject AccessContextService before the @DubboReference proxy is registered, Spring fails to find the bean.

The article provides a detailed analysis of both annotation mechanisms, tracing through the Spring bean lifecycle and Dubbo's reference resolution process. It explains how @DubboReference creates ReferenceBean objects and registers them with the BeanFactory, but only after the owning bean is created.

The solution involves using a BeanFactoryPostProcessor to modify the bean definition of the problematic bean (accessContextResolver) to depend on the bean that contains the @DubboReference (proxyCenter), ensuring proper initialization order. The article also notes that this issue has been resolved in Dubbo 3.0.0 through improved initialization strategies.

Key takeaways include understanding the timing differences between annotation-based injection mechanisms and the importance of bean initialization order in complex dependency scenarios.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaMicroservicesBackend DevelopmentDubboSpring Bootdependency-injectionAutowiredbean-lifecycleDubboReference
政采云技术
Written by

政采云技术

ZCY Technology Team (Zero), based in Hangzhou, is a growth-oriented team passionate about technology and craftsmanship. With around 500 members, we are building comprehensive engineering, project management, and talent development systems. We are committed to innovation and creating a cloud service ecosystem for government and enterprise procurement. We look forward to your joining us.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.