Spring InitializingBean and DisposableBean example

Source: Internet
Author: User

In Spring, InitializingBean and DisposableBean are two marker interfaces, a useful way for Spring to perform certain actions upon bean initialization and destruction. for bean implemented InitializingBean, it will run afterPropertiesSet () after all bean properties have been set. for bean implemented DisposableBean, it will run destroy () after Spring container is released the bean. example [java] packag E org. kodejava. example. spring; import org. springframework. beans. factory. disposableBean; import org. springframework. beans. factory. initializingBean; public class InitDisposeService implements InitializingBean, DisposableBean {/*** Do some processes. */public void doSomething () {System. out. println ("InitDisposeService. doSomething ");}/*** Initialize bean after property set. ** @ throws Exception * // @ Override public void afterPropertiesSet () throws Exception {System. out. println ("InitDisposeService. afterPropertiesSet ");}/*** Clean-up bean when the context is closed. ** @ throws Exception */@ Override public void destroy () throws Exception {System. out. println ("InitDisposeService. destroy ") ;}}here's an example to show you how to use InitializingBeanand DisposableBean. A CustomerServic E bean to implement both InitializingBean and DisposableBean interface, and has a message property. File: Spring-Customer.xml [html] <? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id = "service" class = "org. kodejava. example. spring. initDisposeService "/> </beans> Run it [java] package org. kodejava. example. spring; import org. springfram Ework. context. configurableApplicationContext; import org. springframework. context. support. classPathXmlApplicationContext; public class InitDisposeDemo {public static void main (String [] args) {ConfigurableApplicationContext context = new ClassPathXmlApplicationContext ("InitDispose. xml "); InitDisposeService service = (InitDisposeService) context. getBean ("service"); service. doSomething (); context. cl Ose () ;}} The specified ableapplicationcontext. close () will close the application context, releasing all resources and destroying all cached singleton beans. it's use fordestroy () method demo purpose only :) Output [html] InitDisposeService. afterPropertiesSet InitDisposeService. doSomething InitDisposeService. destroy The afterPropertiesSet () method is called, after the message property is set; while The destroy () method is call after the context. close (); Thoughts... I wocould not recommend to use InitializingBean and DisposableBean interface, because it will tight coupled your code to Spring. A better approach shoshould be specifying theinit-method and destroy-method attributes in your bean configuration file. see http://blog.csdn.net/gloomuu/article/details/8588380

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.