Want to know if we can use spring prototype bean with try-with-resources mechanism:
MyResource implements AutoCloseable
Suppose I have a bean defined as:
@Configuration
public class ResourceCreation {
@Bean()
@Scope("prototype")
public MyResource createResource() {
return new MyResource("Test");
}
}
I have a factory class to generate the prototype beans:
public class ResourceFactory() {
@Autowired
ApplicationContext m_applicationContext;
public static generateResource() {
return m_applicationContext.getBean(MyResource.Class);
}
}
Then to use try-with-resources:
try (MyResource = ResourceFactory.generateResource()) {
...
}
So in this way, is there any problem? Because spring container should manage the lifecycle of the prototype bean.
Will the close() method in the MyResource gets called after the try block ends?