4

What could be a possible example use of destroy method in a real world application? Why would a running application want to destroy its beans? If the beans are created by the spring container for the web application by, say, ContextLoaderListener, then how can these beans be recreated, because the container already started. Is there a way to restart the spring IoC container without restarting the Application server?

user1539343
  • 1,569
  • 6
  • 28
  • 45

3 Answers3

4

One example would be a DataSource or any resource that needs clean up. you might have something like this:

@Bean(destroyMethod = "close")
DataSource dataSource() {
 ....
}

This can be especially important in environments with multiple classloaders like an application server to prevent memory leaks.

It is sometimes the case that doing this is redundant since the underlying resource may do its own cleanup (say as part of a contextDestroyed event in the servlet lifecycle), but you should always verify this.

These spring docs are a useful reference as well. The example cited there is similar:

<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>

Then the bean class:

public class ExampleBean {

    public void cleanup() {
        // do some destruction work (like releasing pooled connections)
    }
}

It is possible to refresh a context. This answer provides a good explanation of that and when you might want to do it.

Community
  • 1
  • 1
leeor
  • 17,041
  • 6
  • 34
  • 60
1

bellow is my views:

What could be a possible example use of destroy method in a real world application?

For most applications, maybe we focus on real businesses mostly, so we won't often meet the scenes where need to define destroy method. But when you meet some basic components or middlewares, you need to pay attention to Resource Management, such as Database Connection, Memory Usage, Disk Usage, etc. You must know how to release unnecessary resource holdings clearly, or this will cause serious problems.

If the beans are created by the spring container for the web application by, say, ContextLoaderListener, then how can these beans be recreated, because the container already started?

Spring Container isn't only for web application, it can serve for common java application(Main Application). Spring Container has two different Bean Type(Singleton and Prototype), Singleton Bean only be pre-instantiated when Spring Container finish to start up, while Prototype Bean will be instantiated by invoking getBean every time.

Is there a way to restart the spring IoC container without restarting the Application server?

Spring IoC Container starts to be instantiated by invoking AbstractRefreshableApplicationContext.refresh() method. This method will destroy entire Spring IoC Container if you have instantiated the Container before. So you can invoke this method to re-instantiate the Container. If you want to understand Spring IoC Mechanism, I suggest you read the Spring's source code: spring-core, spring-beans, spring-context.

Hope to help you.

haolin
  • 216
  • 1
  • 5
0

I have seen beans in a Spring application that either directly or indirectly started non daemon threads. Then it became impossible to stop the process without killing the process. This affected some Jenkins jobs that handled automated testing. So there are plenty of real world examples especially in a DevOps world

EdH
  • 4,918
  • 4
  • 24
  • 34