Hello guys I am trying to understand how exactly prototype Scope works in Spring IoC.
For prototype beans I tried to understand by:
1. Reading this again and again but could not understand fully(https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-scopes-other-injection)
2. Tried to debug via source code,only understood one thing if we do not specify proxyMode, it will not create proxy for prototype bean.
3. default ScopedProxyMode for prototype scope is DEFAULT which typically equals NO.. unless configured in component scan.
So I have 3 examples here:
Case A: Prototype bean used independently(not having any dependency or is not dependency for any bean) without proxymode defined
@Bean
@Scope("prototype")
public Employee employee(){
return new Employee();
}
Case B: Prototype bean inside a singleton bean as dependency without proxymode defined
@Bean
@Scope("prototype")
public Employee employee(){
return new Employee();
}
@Bean
@Scope("singleton")
public Department department(){
return new Department();
}
Case C: Prototype bean inside a singleton bean as dependancy with proxymode defined
@Bean
@Scope("prototype",proxyMode= ScopedProxyMode.TARGET_CLASS)
public Employee employee(){
return new Employee();
}
@Bean
@Scope("singleton")
public Department department(){
return new Department();
}
Guys questions here are:
- How will case A, case B and case C work?
- I am trying to understand what exactly will be correct way.
- Also not specifying proxymode is correct or not?
- What happens when proxyMode is not set? how does prototype work then?
What I typically saw in implementation examples on internet is people often do not configure proxyMode either mistakenly or are not aware about this option or maybe they are right.
Thank you for your valuable time.