Skip to content

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

@mattmok

Description

@mattmok

Pre-check

  • I am sure that all the content I provide is in English.

Search before asking

  • I had searched in the issues 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:
    @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:

@Configuration
public class DemoServiceConfigure {
    @Bean
    @DubboReference(url = "tri://172.17.0.2:50052?serialization=fastjson2")
    public ReferenceBean<DemoService> demoServiceReferenceBean() {
        ReferenceBean<DemoService> 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

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:

beanDefinition.setAttribute("interfaceClass", beanClass);
beanDefinition.setAttribute("interfaceName", interfaceName);

However, it only adds id to beanDefinition.getPropertyValues() (no interface-related properties)
In ReferenceBean.afterPropertiesSet():

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):

// 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions