Skip to content

Spring AOT processed application does not properly initialize Prototype Bean #35871

@ssserj

Description

@ssserj

Repository that reproducing the error - https://github.com/ssserj/spring-demo
I did not check the app on previous versions 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

Produce 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions