apache / apache/dubbo

[Bug] Native Image Startup Error with ReferenceBean + @DubboReference Configuration

Open
#16,145 5 comments 0 reactions 0 assignees View on GitHub
status/waiting-for-feedback
Dominant language
Java
Stars
41.6k
Forks
26.4k
Avg merge
15h 13m
Merged PRs (30d)
4

Description

### Pre-check

- [x] I am sure that all the content I provide is in English.

### Search before asking

- [x] I had searched in the [issues](https://github.com/apache/dubbo/issues?q=is%3Aissue) and found no similar issues.

### Apache Dubbo Component

Java SDK (apache/dubbo)

### Dubbo Version

Dubbo Java 3.3.6, GraalVM 17.0.18+8.1, Spring-boot: 3.5.11, Ubuntu 20.04

### Steps to reproduce this issue

Here's a well-structured English issue report for the Apache Dubbo GitHub repository, following open-source community best practices:
markdown
## Issue Description
### Environment
- Dubbo Version: latest (from dubbo-samples-native-image)
- Native Image Build Tool: GraalVM Native Image
- Build Command: `mvn clean package -Pnative native:compile`
- Spring Boot Version: compatible with Dubbo samples

### Steps to Reproduce
1. Use the official Dubbo native image consumer sample: https://github.com/apache/dubbo-samples/tree/master/2-advanced/dubbo-samples-native-image/dubbo-samples-native-image-consumer
2. **Working Scenario**: Using `@DubboReference` directly on field works fine for native image compilation:
```java
@DubboReference(url = "tri://172.17.0.2:50052?serialization=fastjson2")
private DemoService demoService;```
Native image builds and runs successfully.
Failing Scenario: Switch to ReferenceBean configuration with @Bean + @DubboReference:
```java
@Configuration
public class DemoServiceConfigure {
@Bean
@DubboReference(url = "tri://172.17.0.2:50052?serialization=fastjson2")
public ReferenceBean demoServiceReferenceBean() {
ReferenceBean referenceBean = new ReferenceBean<>();
return referenceBean;
}
}
```
mvn clean spring-boot:run works normally (JVM mode)
Native image compilation succeeds, but application startup fails with error:
Error Log
```plaintext
Caused by: java.lang.IllegalArgumentException: The interface class of ReferenceBean is not initialized
at org.apache.dubbo.config.spring.ReferenceBean.afterPropertiesSet(ReferenceBean.java:XXX)
...
```
Root Cause Analysis
I traced the issue to ReferenceAnnotationWithAotBeanPostProcessor:
In processReferenceAnnotatedBeanDefinition() method:
The method correctly sets interfaceClass and interfaceName as attributes on beanDefinition:
```java
beanDefinition.setAttribute("interfaceClass", beanClass);
beanDefinition.setAttribute("interfaceName", interfaceName);
```
However, it only adds id to beanDefinition.getPropertyValues() (no interface-related properties)
In ReferenceBean.afterPropertiesSet():
```java
if (AotWithSpringDetector.useGeneratedArtifacts()) {
// Native image mode uses property values (only has "id")
this.interfaceClass = (Class)beanDefinition.getPropertyValues().get("interfaceClass");
this.interfaceName = (String)beanDefinition.getPropertyValues().get("interfaceName");
} else {
// JVM mode uses attributes (works fine)
this.interfaceClass = (Class)beanDefinition.getAttribute("interfaceClass");
this.interfaceName = (String)beanDefinition.getAttribute("interfaceName");
}
```
In native image mode (useGeneratedArtifacts() = true), the code tries to read interfaceClass/interfaceName from propertyValues (which are missing)
This causes interfaceClass to be null, triggering the IllegalArgumentException

### What you expected to happen

### Expected Behavior
ReferenceBean configuration with @Bean + @DubboReference should work for native image builds (same as field-based @DubboReference)
Interface metadata should be properly propagated to ReferenceBean in AOT/native mode
Proposed Fix
Modify ReferenceAnnotationWithAotBeanPostProcessor.processReferenceAnnotatedBeanDefinition() to add interfaceClass and interfaceName to beanDefinition.getPropertyValues() (in addition to attributes):
```java
// Add these lines after setting attributes
beanDefinition.getPropertyValues().add("interfaceClass", beanClass);
beanDefinition.getPropertyValues().add("interfaceName", interfaceName);
```
This ensures the properties exist in both attribute and property value storage, making them accessible in native image mode.

### Anything else

_No response_

### Do you have a (mini) reproduction demo?

- [ ] Yes, I have a minimal reproduction demo to help resolve this issue more effectively!

### Are you willing to submit a pull request to fix on your own?

- [ ] Yes I am willing to submit a pull request on my own!

### Code of Conduct

- [x] I agree to follow this project's [Code of Conduct](https://www.apache.org/foundation/policies/conduct)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.