
We can define the init and destroy methods globally as well. The local init() and destroy() methods can be configured to a specific bean as in the given example. Global definitions applicable to all beans defined in whole beans context.Local definitions applicable to a single bean.We can add the default init() and destroy() methods in two ways: Private ApplicationContext void setApplicationContext(ApplicationContext ctx) Public class DemoBean implements ApplicationContextAware Java program to show the use of Aware interfaces. Set the ServletContext that this object runs in. Void setServletContext (ServletContext servletContext) Set the ServletConfig that this object runs in. Void setServletConfig (ServletConfig servletConfig) Set the ResourceLoader that this object runs in. Void setResourceLoader (ResourceLoader resourceLoader) Set the PortletContext that this object runs in. Void setPortletContext (PortletContext portletContext) Set the PortletConfig this object runs in. Void setPortletConfig (PortletConfig portletConfig) Set the NotificationPublisher instance for the current managed resource instance. Void setNotificationPublisher (NotificationPublisher notificationPublisher) Set the MessageSource that this object runs in. Void setMessageSource (MessageSource messageSource) Set the LoadTimeWeaver of this object’s containing ApplicationContext. Void setLoadTimeWeaver (LoadTimeWeaver loadTimeWeaver) Set the BootstrapContext that this object runs in. Void setBootstrapContext (BootstrapContext bootstrapContext) Set the name of the bean in the bean factory that created this bean. Void setBeanFactory (BeanFactory beanFactory) throws BeansException Ĭallback that supplies the owning factory to a bean instance. Void setBeanClassLoader (ClassLoader classLoader) Ĭallback that supplies the bean class loader to a bean instance. Set the ApplicationEventPublisher that this object runs in. Void setApplicationEventPublisher (ApplicationEventPublisher applicationEventPublisher) Interface to be implemented by any object that wishes to be notified of the ApplicationContext that it runs in. Void setApplicationContext (ApplicationContext applicationContext) throws BeansException We can summarize these interfaces as : Aware interface 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.

Bean annotation in spring boot code#
Second you generally use it to configure beans in Java code (if you are not using xml configuration) and then call it from a class using theĪpplicationContext.getBean method.Spring framework provides the following four ways for controlling life cycle events of a bean: I see a lot of answers and almost everywhere it's mentioned is for autowiring where component is scanned, and is exactly declaring that bean to be used differently.
