How to initialize and destroy beans in Spring?

Source: Internet
Author: User

When creating an instance of a bean you might need to do some initialization to the bean. likewise, when the bean is no longer needed and removed from the Spring container you might want to do some cleanup routine or destroy the bean. to do this initialization and destroy routine you can use the init-method anddestroy-method attribute when declaring a bean in spring configuration using the <bean> ele Ment. by defining the init-method and destroy-method it will allow the Spring Container to call the initialization method right after the bean created. and just before the bean removed and discarded from the container, the defined destroy method will be called. let's see some code snippet as an example. [java] package org. kodejava. example. spring; public class DemoEngine {public void demoInitialize () {System. out. println ("DemoEngine. demoInitialize ");} public void demoDestroy () {System. out. println ("DemoEngine. demoDestroy ") ;}} Below is the Spring configuration file that we use to declare the bean. you'll see in the configuration there are additional attributes that we add to the bean. [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 = "engine" class = "org. kodejava. example. spring. demoEngine "init-method =" demoInitialize "destroy-method =" demoDestroy "/> </beans> Create a small program to execute our demo: [java] package org. kodejava. example. spring; import org. springframework. context. configurableApplicationContext; import org. springframework. context. support. classPathXmlApplicationContext; public class InitDestroyDemo {public static void main (String [] args) {ConfigurableApplicationContext context = new ClassPathXmlApplicationContext ("AutoEngine. xml "); AutoEngine engine = (AutoEngine) context. getBean ("engine"); // context. close will remove the bean from the container. // This will call our bean destroy method. // context. close () ;}} When you run the program it will print the following output: [html] DemoEngine. demoInitialize DemoEngine. demoDestroy The advantage of using this method to initialize or clean up the bean is that it doesn't mandate our bean to implement or extend any Spring API which will make our bean reusable on other container beside spring.

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.