-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Description
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.
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