Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - How can I destroy my prototype-scoped beans?

I've got a singleton bean that has a method that creates instances of a prototype bean. I'm using the method documented here to get instances of the prototype bean.

public class SingletonService implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    public void go() {

        MyPrototypeBean prototype = applicationContext
            .getBean(MyPrototypeBean.class);

        prototype.doSomething();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
        throws BeansException {

        this.applicationContext = applicationContext;
    }
}

At first I thought that this was good enough, that my instance of 'prototype' would go out of scope when the 'go' method returned, meaning that the instance would have not reference and would be garbage collected.

However, a peer pointed out the following statement from the documentation:

The client code must clean up prototype-scoped objects and release expensive resources that the prototype bean(s) are holding.

So it sounds like Spring will retain a reference, and so the gc will never pick it up? If that's the case how do I tell Spring to release the reference? The documentation mentions that I can use a 'custom bean post-processor', but it's not clear where that processor would fit in and what code it would run.

Thanks all in advance for helping out, Roy

like image 729
Roy Truelove Avatar asked Dec 23 '11 17:12

Roy Truelove


People also ask

How do you destroy a Java Bean?

We can use init-method and destroy-method attribute in bean configuration file to mark init method and destroy method in Java Bean. The init-method is called during initialization and the destroy-method is called during destruction.

What is scoped bean injection problem?

By default, Spring beans are singletons. The problem arises when we try to wire beans of different scopes. For example, a prototype bean into a singleton. This is known as the scoped bean injection problem.

What is destroy method in spring?

The init-method attribute specifies a method that is to be called on the bean immediately upon instantiation. Similarly, destroymethod specifies a method that is called just before a bean is removed from the container.

What is JVM scoped bean?

Single pattern in java mean you can create the only one instance of a that class in JVM. But In spring singleton bean scope means every container can create only single bean in the Spring IoC Container but a JVM can have multiple Spring IoC Container so JVM can multiple beans rather than bean singleton bean scope.


1 Answers

I think you're misunderstanding the documentation. It's just saying Spring won't manage the lifecycle of the prototype bean, so @PreDestroy (etc.) methods need to be called by your own code. Spring won't retain a reference.

like image 123
artbristol Avatar answered Sep 28 '22 04:09

artbristol