spring-projects / spring-projects/spring-framework

Autowiring not working for native applications with prototype components

Open
#36,649 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: core status: waiting-for-triage theme: aot
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

If you have a Spring Component with a Scope of "prototype" and the Component has a constructor with some parameters, when that Component is created using BeanFactory in a native application, any Autowired member variables are not populated and no errors are generated. However, the same application run normally works as expected.

To replicate,

  1. Spring Initialzr to create an project containing "GraalVM Native Support" and "Spring Web"
  2. Change version of "org.graalvm.buildtools.native" plugin to "1.0.0"
  3. Create DemoComponent class:
@Component
@Scope("prototype")
public class DemoComponent {
    @Autowired
    public BeanFactory beanFactory;

    public DemoComponent(Object obj) {

    }
}
  1. Create DemoController class:
@RestController
public class DemoController {

    @Autowired
    protected BeanFactory beanFactory;

    @RequestMapping
    public void demo() {
        DemoComponent demoComponent = beanFactory.getBean(DemoComponent.class, new Object());
        System.out.println("HERE1: " + demoComponent.beanFactory);
    }
}
  1. Build using gradlew bootJar
  2. Generate reachability-metadata.json using java -agentlib:native-image-agent=config-output-dir=src\main\resources\META-INF\native-image -jar build\libs\demo-0.0.1-SNAPSHOT.jar
  3. Use browser to go to "http://localhost:8080" and confirm that beanFactory of DemoComponent is populated
  4. Build and run native application using gradlew nativeRun
  5. Use browser to go to "http://localhost:8080" and confirm beanFactory of DemoComponent is not populated

Note that if I remove the parameter from the DemoComponent constructor and from the beanFactory.getBean(... line in the DemoController, the beanFactory in the DemoComponent is populated as expected in both native and bootJar.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the DemoComponent and DemoController reproduction, then compare the behavior of bootJar with nativeRun using the generated reachability-metadata.json. The fix is complete when the prototype component's autowired BeanFactory is populated in the native application as it is in the regular run.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.