Spring Bean Lifecycle
Table of Contents
Part 1
Before the application is deemed ready for use, Spring will go through and carry out all these actions on each bean in the context.
This provides you with a variety of connections. You do have a great deal of flexibility and control over this. Sincerely speaking, you hardly ever need to hook into this. There are use cases that you encounter, but it's rare that you require this much control over the production of the beans. You're typically doing something a little complicated when you do it. But I believe that awareness is the most important lesson to learn. Use this when you need it; it is there and available for you. So here this is just the bean becoming ready for use.
Part 2
There is also a lifecycle for the beans to be terminated. So when the container is being shut down, the application receives some type of shutdown event. There is a lifecycle for the shutdown. So you can annotate a bean method with the annotation PreDestroy that will get annotated. And then you can also implement a disposable bean interface that provides a destroy method so that the annotation will come first and then the destroy method. And at that point we call the bean terminated.
Callback Interfaces
Consequently, we did discuss a few of the interfaces. There are two interfaces in Spring that you can use to implement callback events. You have the afterPropertiesSet from the InitializingBean. So if you implement that interface, that method will be called. Finally, there is the DisposalBean. We discussed the fact that that is one of the last functions called before the bean is terminated.
Lifecycle Annotations
You do also have a couple annotations, and these were in that flowchart as well. We have PostConstruct, so the bean has been constructed, but it has not been returned to the requesting object. And then we also have PreDestroy. So if you remember in the termination workflow, this was called just before the interface method is called.
Bean Post Processors
These can be useful if you're working with a third-party object of some kind that you need to create, put into your context, and update in some way. like configuring a component that is typically not managed by Spring. So, you now have some capabilities. The main point you should take away from this post is that bean processors are referred to as such internationally. In essence, you receive this event, and it will be examined. If you implement it, this will be called 100 times in the case where you have 100 beans and every object or bean. In order to implement this typically, you're probably searching for a particular bean, a kind of bean that
Aware Interfaces
Finally, Spring has over 14 aware interfaces as well. Therefore, these extend interface are also known as aware. Thus, they are a continuation of that. They number fourteen. The Spring framework largely makes use of these. Therefore, if you're creating Spring applications, you should be aware that you use these. Therefore, if you have a use case, you may encounter situations where you must use these. These might be very useful.
These are some of the current ones:
Aware Interface | Description |
ApplicationContextAware | Interface to be implemented by any object that wishes to be notified of the ApplicationContext that it runs in. |
ApplicationEventPublisherAware | Set the ApplicationEventPublisher that this object runs in. |
BeanClassLoaderAware | Callback that supplies the bean class loader to a bean instance. |
BeanFactoryAware | Callback that supplies the owning factory to a bean instance. |
BeanNameAware | Set the name of the bean in the bean factory that created this bean. |
BootstrapContextAware | Set the BootstrapContext that this object runs in. |