Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Destrction method of bean lifecyle in spring

    • 0
    • 2
    • 1
    • 3
    • 0
    • 0
    • 0
    • 0
    • 246
    Comment on it

    Destruction Methods:-The destruction method of a spring bean container is called when bean is removed from the spring container.

    Example:
    Address.java

    package com.chandan;
    
    public class Address {
    
        private String city;
    
        public Address()
        {
    
        }
        public String getCity() {
            return city;
        }
    
        public void setCity(String city) {
            this.city = city;
        }
    
    }
    

    Teacher.java

    package com.chandan;
    
    public class Teacher {
        private String name;
        private int id;
        private Address address;
    
        public Address getAddress() {
            return address;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
    
        public void destroy()
        {
            System.out.println("Bean is Destroyed Now");
        }
    
    }
    

    App.java

    package com.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.babita.Department;
    import com.babita.Employee;
    import com.chandan.Teacher;
    import com.manish.Course;
    import com.manish.Student;
    
    public class App {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            AbstractApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"app-context.xml"});
    
    
            Teacher teach=(Teacher)ctx.getBean("teacher");
    
    
            System.out.println("Id:="+teach.getId());
            System.out.println("Name:="+teach.getName());
            System.out.println("City:="+teach.getAddress().getCity());
            ctx.registerShutdownHook();
    
        }
    

    app-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <context:annotation-config/>
    
        <bean id="address" class="com.chandan.Address">
            <property name="city" value="Dehradun"/>
        </bean>
    
        <bean id="teacher" class="com.chandan.Teacher" destroy-method="destroy" >
            <property name="id" value="1001"></property>
            <property name="address" ref="address"></property>
            <property name="name" value="Chandan Ahluwalia"></property>
    
        </bean>
    
    
    </beans>
    

    Output:

    Id:=1001
    Name:=Chandan Ahluwalia
    City:=Dehradun
    Bean is Destroyed Now
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: