- Initialization phase - ApplicationContext creation phase: Begins with the creation of ApplicationContext - BeanFactory Initialization phase : 1. Init Bean Definition phase : Load bean definitions and metadata - Bean definitions are loaded from all sources (Java configuration, xml configuration, component scanning and auto configuration) - Id is used to create index for the factory. Everything else is referenced as type or name - BeanFactory only contains reference and metadata. Nothing is yet instantiated. (No object created) - example : `ApplicationConfig` class is read by the system to know the registered beans 2. Init post- Bean Definition phase: Modify beans - Perform work on entire BeanFactory - Can transform any bean or modify bean behavior in the BeanFactory before object creation - `Interface BeanFactoryPostProcessor` is used for this purpose. Bean definitions that use this must be `static` to make sure they act like singletons (to avoid risk side-effects of dynamic behavior) - helps to write custom component to impact the BeanFactory - Bean and system configuration is completed - example: loading information from `application.properties` file 3. Bean Instantiation phase: Instantiate objects by constructor injection - Beans are instantiated in the factory using constructors - So, objects are constructed. But they are not available for use yet. - Done in correct order to ensure that the dependencies are created first - Singleton beans remains in the factory as they are created (eager instantiation) and their handle remains in BeanFactory unless IoC container is closed (to make sure the beans are available across the whole application) - Beans that are not singletons are not created unless they are needed (lazy instantiation) and mostly their handle is released by IoC container (So that they are easily disposed off by garbage collector once they are no longer needed) 4. Post instantiation dependency injection phase: optional dependency by setter injection - Setters are called - `Autiwired` on non-constructors are called (fields and setters) - All dependencies are injected - Beans are yet not ready to use 5. Post-processing of Bean before serving the object to the runtime: - Final point of configuration manipulation - Additional behaviors added to the beans - `Interface BeanPostProcessor` is used to inject common behavior to a class of beans - Beans are finally ready to use - 3 sub-phases: 1. Before initializer : proxies are built 2. Initializer : `@PostConstruct` methods called here 3. After initializer - Use phase: - ApplicationContext serves proxies to the original class - ApplicationContext maintains handle of all singleton Beans (they are not garbage collected) - `Interface ApplicationContextAware` can be used to give the beans insight about application (gives that class a handle to the ApplicationContext) - Destruction phase - Begins when close is called on ApplicationContext - Any `@PreDestroy` is called and then the beans are marked as available for garbage collection - Once closed, context cannot be re-used again (even if the garbage collector hasn't taken the beans yet) - Difference among Java, xml and auto configuration: - Java Configuration: - instantiation of all beans along with constructor and setter injection together - `@Bean` annotated methods are executed in proper order. - XML injection - instantiation of all beans and constructor injection simultaneously - setter, property injection - Auto configuration: - instantiation of all beans through scanning - `@Autowired` on constructors injection (if there is only one constructor, then it will be the default autowired) - `@Autowired` on setter and field injection - Method during object instantiation : - You want to perform some task before behaviors are applied in runtime - `@PostConstruction` annotation is used - must be a void method with no parameters. eg: `public void init(){}` - example use case : pull some data in database before anything begins - Method after object destruction has started : - You want to perform some task before spring goes out of scope - `@PreDestroy` annotation is used - must be a void method with no parameters. eg: `public void destroy(){}` - called after property settings - example use case : backup data before application shuts down