0

Let's say I have 2 beans, Bean A and Bean B. Bean A is a dependency of Bean B. Let's assume I have specified a destroy method for Bean A but not for Bean B. By logic, Bean B should first be destroyed and then Bean A should be destroyed on shutdown. Does Bean B ceases to exist even if no destroy method is specified? If Bean B actually doesn't get destroyed then does Bean A also not get destroyed even though a destroy method has been specified for it?

CS1999
  • 303
  • 2
  • 10
  • 1
    This sounds like something you could reproduce yourself. Have you tried? – M.P. Korstanje Sep 06 '22 at 09:28
  • "*on shutdown ... Does Bean B ceases to exist ... ?*" Everything ceases to exist on shutdown... – Michael Sep 06 '22 at 09:29
  • Hi, I am not sure how to check if a bean has been destroyed or not. The application I am working on is very vast and I am having troubles reproducing this. By shutdown I mean ApplicationContext shutdown – CS1999 Sep 06 '22 at 09:31

1 Answers1

1

Think of it that method destroy() always exists for each bean. But by default it is empty. If you need to have some logic executed upon Bean destruction beyond what is done by default than you override this method with your own logic. But just you didn't override such method for some beans does not mean that they are not destroyed.

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • When you say it is destroyed, is there any change in it's structure ? Or is it just marked as 'destroyed' ? It's still in memory? I guess I am trying to understand what's the main difference between before and after being destroyed when I don't override the destroy method – CS1999 Sep 06 '22 at 12:04
  • when it is destroyed it is marked for garbage collection and no longer accessible to you at all. So, destroyed really means destroyed. It no longer exists. method destroy is only needed if you want additional logic to be executed beyond standard destruction. Say you open some socket that works with IO outside of your JVM. You will need to close it. Such a thing you might put in your destroy method as it won't be done with default destruction – Michael Gantman Sep 06 '22 at 12:22
  • Is there any documentation that specifically states this ? – CS1999 Sep 06 '22 at 12:26
  • What if a reference to that bean still exists somewhere in the program? – CS1999 Sep 06 '22 at 12:33
  • Spring won't run destroy of the class is still needed. destroy is similar to Garbage collection - it is an internal mechanism of cleanup of objects that have no more references. You don't have to worry about it. However, only if you created some resources that will not be cleaned by the system you will have to override destroy method to take care of custom clean up. It is actually very rarely needed – Michael Gantman Sep 06 '22 at 13:02
  • Could you point me to the source code for this? I am seeing other conflicting answers on SO which say otherwise. – CS1999 Sep 07 '22 at 02:40
  • https://stackoverflow.com/questions/72563048/what-happens-to-spring-bean-after-applicationcontext-is-shutdown – CS1999 Sep 07 '22 at 02:41