Spring Boot Version : 3.2.3

I will explain a problem below. But this problem only occurs when I build with Spring Native. When I run it as a normal spring application, my code works correctly. The problem only occurs when I build with Spring Native.

I have problems when I pass another prototype bean as a parameter to prototype beans. When I examined the code, I saw that it did not use the bean I gave as a parameter. Therefore, instead of the parameter bean I give, Spring itself creates a bean and uses it.

Capture

I will also try to explain with an example. Let's say I have an A bean and this A bean is a prototype bean. I also have a B bean, and in my B bean is a prototype bean. Also, let's assume that bean B takes bean A as a parameter. I create an A bean using ApplicationContext. (Example Memory location: #666666) Again, I create B bean using ApplicationContext and while creating it, I give the A bean (memory location: #666666) as a parameter. Later, when I check the A bean inside the B bean, I see a bean with a different location in the memory. So I cannot see the bean I gave as a parameter. I see that it has a different location in Memory. (For example: Memory Location : #777777)

When I examine the code, if there is a supplier instance, it creates the bean without taking into account the parameter I give. I think that's the problem. Can you help me?

Example Code:

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public CustomerService customerService(OrderProperties orderProperties) {
        return new CustomerService(orderProperties);
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public OrderProperties orderProperties() {
        return new OrderProperties();
    }

    public void createService() {
        OrderProperties orderProperties = applicationContext.getBean(OrderProperties.class);
        System.out.println("OrderProperties Memory Address : " + orderProperties);          // #666666
        CustomerService customerService = applicationContext.getBean(CustomerService.class, orderProperties);
        System.out.println("OrderProperties in CustomerService Memory Address : " 
        + customerService.getOrderProperties());   // #777777
    }

The Related Code Link : https://github.com/omercelikceng/prototype-bean-problem

Comment From: snicoll

Thanks for the report, this isn't about native per se but the AOT transformation as your screenshot shows. Running with AOT optimizations on the JVM would fail as well.

This is unfortunately a known limitation and is described in the reference documentation.

Comment From: omercelikceng

Thank you very much @snicoll. I read the document but I couldn't understand some points. If you have time, could you explain in more detail why this problem occurs?

Comment From: snicoll

What more details do you need and what does "this problem" means?

Comment From: omercelikceng

I debug the code by giving the spring.aot.enabled=true parameter. Actually, we have the parameter. But we don't use it. Why can't we use the parameter we have? When I read the document, it says that AOT could not detect this. Why can't it detect it? I'm trying to understand, please excuse me.

Comment From: snicoll

That's the first sentence:

Spring AOT detects what needs to be done to create a bean and translates that in generated code using an instance supplier.

Once that has been translated in generated code for optimization (mandatory for native images) you can't at runtime decide to create the bean in a different manner. That's essentially what that section states.

Comment From: omercelikceng

I understand. Thank you very much @snicoll :)