@PostConstruct: Code will execute after constructor and after injection of dependencies
@PreDestroy: Code will execute before bean is destroyed
e.g.
Note: For "prototype" scoped beans, Spring does not call the @PreDestroy method. Gasp!
In contrast to the other scopes, Spring does not manage the complete lifecycle of a prototype bean: the container instantiates, configures, and otherwise assembles a prototype object, and hands it to the client, with no further record of that prototype
instance.
Thus, although initialization lifecycle callback methods are called on all objects regardless of scope,in the case of prototypes, configured destruction lifecycle callbacks are not called. The client code must clean up prototype-scoped objects and release expensive resources that the prototype bean(s) are holding.
To get the Spring container to release resources held by prototype-scoped beans, try using a custombean post-processor, which holds a reference to beans that need to be cleaned up.
9. Spring Configuration with Java Code
Instead of configuring Spring container using XML
Configure the Spring container with Java cod
3 ways of configuring Spring Container
Full XML config
XML Component Scan
Java Configuration Class
e.g. 1
e.g.2
e.g.3
9.1 Development Process
Create a Java class and annotate as @Configuration