when is a spring beans destroy-method called? when is a spring beans destroy-method called? spring spring

when is a spring beans destroy-method called?


Your example doesn't work because you're not shutting down the appcontext, you're just letting the program terminate.

Call close() on the context, and you'll see the bean destroy-methods being called.


It may be too late for the OP, but if someone is still looking for it...

The close method is in AbstractApplicationContext and not ApplicationContext, also another way is to use ctx.registerShutdownHook() instead of ctx.close() for obvious reasons that while running Junits you might want to close the context but not while in production environment so let Spring decide on when to close it.


//Getting application contextApplicationContext context = new ClassPathXmlApplicationContext(beansXML); //cleaning context((ClassPathXmlApplicationContext) context).close();