spring-projects / spring-projects/spring-framework

Spring AOT processed application does not properly initialize Prototype Bean

Open
#35,871 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Repository that reproduces the error: https://github.com/ssserj/spring-demo

I did not check the app on previous versions of spring boot, but here is a typical example of using Prototype Bean:

@Service
@Scope("prototype")
@Slf4j
public class PrototypeBean implements BeanFactoryAware {
    @Getter
    private final String name;
    protected SingletonBean singletonBean;
    protected BeanFactory beanFactory;

    public PrototypeBean(String name) {
        this.name = name;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) {
        log.info("PrototypeBean is called with name " + name);
        this.beanFactory = beanFactory;
    }

    @Autowired
    public void setSingletonBean(SingletonBean singletonBean) {
        this.singletonBean = singletonBean;
    }

    public void process() {
        System.out.println("PrototypeBean is called with name " + name);
        System.out.println("PrototypeBean is called by beanFactory at " + this.beanFactory.getBean(SingletonBean.class).getTime());
        System.out.println("PrototypeBean is called by singletonBean at " + singletonBean.getTime());
    }
}

Spring 3.5.8
Java - 21.0.9
OS - MacOS, Linux

Basic Spring App
mvn clean package
java -jar target/demo-0.0.1-SNAPSHOT.jar

Expected result:

 :: Spring Boot ::                (v3.5.8)
 
2025-11-22T15:35:34.140+03:00  INFO 41206 --- [demo] [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 21.0.9 with PID 41206 (/Users/s.sergeev/Code/demo-spring/target/classes started by s.sergeev in /Users/s.sergeev/Code/demo-spring)
2025-11-22T15:35:34.141+03:00  INFO 41206 --- [demo] [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2025-11-22T15:35:34.272+03:00  INFO 41206 --- [demo] [           main] com.example.demo.DemoApplication         : Started DemoApplication in 0.243 seconds (process running for 0.402)
2025-11-22T15:35:34.274+03:00  INFO 41206 --- [demo] [           main] com.example.demo.service.PrototypeBean   : PrototypeBean is called with name name1
PrototypeBean is called with name name1
PrototypeBean is called by beanFactory at 2025-11-22T15:35:34.274535
PrototypeBean is called by singletonBean at 2025-11-22T15:35:34.274572
2025-11-22T15:35:34.274+03:00  INFO 41206 --- [demo] [           main] com.example.demo.service.PrototypeBean   : PrototypeBean is called with name name2
PrototypeBean is called with name name2
PrototypeBean is called by beanFactory at 2025-11-22T15:35:34.274732
PrototypeBean is called by singletonBean at 2025-11-22T15:35:34.274750
2025-11-22T15:35:34.274+03:00  INFO 41206 --- [demo] [           main] com.example.demo.service.PrototypeBean   : PrototypeBean is called with name name3
PrototypeBean is called with name name3
PrototypeBean is called by beanFactory at 2025-11-22T15:35:34.274879
PrototypeBean is called by singletonBean at 2025-11-22T15:35:34.274891
AOT-processed Spring App
mvn clean package && mvn spring-boot:process-aot
java -Dspring.aot.enabled=true -jar target/demo-0.0.1-SNAPSHOT.jar

Produces error:

 :: Spring Boot ::                (v3.5.8)

2025-11-22T15:36:32.254+03:00  INFO 41605 --- [demo] [           main] com.example.demo.DemoApplication         : Starting AOT-processed DemoApplication using Java 21.0.9 with PID 41605 (/Users/s.sergeev/Code/demo-spring/target/classes started by s.sergeev in /Users/s.sergeev/Code/demo-spring)
2025-11-22T15:36:32.254+03:00  INFO 41605 --- [demo] [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2025-11-22T15:36:32.307+03:00  INFO 41605 --- [demo] [           main] com.example.demo.DemoApplication         : Started DemoApplication in 0.168 seconds (process running for 0.326)
2025-11-22T15:36:32.308+03:00  INFO 41605 --- [demo] [           main] com.example.demo.service.PrototypeBean   : PrototypeBean is called with name name1
PrototypeBean is called with name name1
PrototypeBean is called by beanFactory at 2025-11-22T15:36:32.309653
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.example.demo.service.SingletonBean.getTime()" because "this.singletonBean" is null
	at com.example.demo.service.PrototypeBean.process(PrototypeBean.java:58)
	at com.example.demo.service.ProcessorBean.process(ProcessorBean.java:35)
	at com.example.demo.DemoApplication.main(DemoApplication.java:15)

BeanFactory works correctly. Explicit declaration and invocation of singletonBean from prototypeBean does not work in AOT processed application.

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 reproducer's PrototypeBean.java, ProcessorBean.java, and DemoApplication.java, then run the basic and AOT Maven commands described in the issue. Compare the initialization paths around the failing singletonBean access. Done means the AOT-processed application runs all three prototype calls without the NullPointerException and initializes singletonBean as the regular application does.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.