Spring offers a range of interfaces that allow the beans to indicate to the container that they require a particular infrastructure dependency.Įach of these Aware interfaces will require us to implement a method to inject the dependency in the bean. *Aware Interfaces to Add Specific Behavior Bean initialization void destroy() throws Exception Other bean attributes and void afterPropertiesSet() throws Exception Public class DemoBean implements InitializingBean, DisposableBean The DisposableBean interface specifies a single method: void destroy() throws Exception Ī sample bean implementing the above interfaces would look like this: package Similarly, implementing the .DisposableBean interface allows a bean to get a callback before the Spring container destroys the bean. A better approach is to use “ init-method” attribute in bean definition in applicationContext.xml. The afterPropertiesSet() method is not a preferable way to initialize the bean because it tightly couples the bean class with the spring container. The InitializingBean interface specifies a single method: void afterPropertiesSet() throws Exception The .InitializingBean interface allows a bean to perform initialization work after all necessary properties on the bean have been set by the container. InitializingBean and DisposableBean Interfaces ![]() Let’s learn about each way in some detail. Custom init() and destroy() methods in bean configuration file.*Aware interfaces for specific behavior.InitializingBean and DisposableBean callback interfaces.Spring framework provides the following four ways for controlling life cycle events of a bean:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |