when will a prototype bean get garbage collected if we use proxy mode for scope prototype for a class in Spring boot, is it handled by Spring?
when the object of MyClassB will get garbage collected in below example or is it leading to a memory leak?
@Service
public class MyClassA {
@Autowired
private MyClassB myClassB;
public String findMydata(String input) {
String myData = myClassB.getSomeData(input);
return myData;
}
}
Below is the class with scope prototype which is used by above class
@Service
@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE,
proxyMode=ScopedProxyMode.TARGET_CLASS)
public class MyClassB {
MyPojoClassA mypojoA = null;
MyPojoClassB mypojoB = null;
@Autowired
private MyClassC myClassC;
@Autowired
private myClassD myClassD;
public String getSomeData(String input) {
String SomeData = "";
myMethodA(input);
// makes call to external service for data
SomeData = myClassC.getSomeData(mypojoA.getSomething());
myMethodB(mypojoA.getSomeOthervalue());
if(SomeData.isBlank()){
// retrieve the data from database.
SomeData = myClassD.getSomeData(mypojoB.getSomething());
}
return SomeData;
}
private void myMethodA(String input){
// process and set values in mypojoA
}
private void myMethodB(String input){
// process and set values in mypojoB
}
}
The usage and assigning values to different fields of myPojoA and myPojoB is done multiple times by calling different private methods inside method getSomeData. here in code sample I have not made those calls for simplicity, but have to show that there are two instance level reference variables of two classes being used.