Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Spring @PostConstruct and @PreDestroy

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 930
    Comment on it

    Spring Framework @PostConstruct and @PreDestroy annotations are used to just like life cycle callback methods. @PostConstruct and @PreDestroy are used widely for the process of bean initialization and to free resources at the time of bean destruction respectively.

    @PostConstruct annotation marks the method that this method will be called just after the bean has been initialized. It is called when bean is fully initialize with all dependencies injected.

    @PostConstruct annotation marks the method that this method will be called just before the bean is destroyed. This method is used for important tasks like clean-ups resources before the bean has been destroyed.

    Spring @PostConstruct and @PreDestroy Example:

    bean.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:c="http://www.springframework.org/schema/c"
       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="personBean" class="com.evon.example.callbackWithAnnotation.Person"/>
    </beans>
    

    Person.java

    package com.evon.example.callbackWithAnnotation;
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    
    public class Person {
        private String personName="Tom";
        private String country="USA";
    
        public String getPersonName() {
            return personName;
        }
    
        public void setPersonName(String personName) {
            this.personName = personName;
        }
    
        public String getCountry() {
            return country;
        }
    
        public void setCountry(String country) {
            this.country = country;
        }
    
        @PostConstruct
        public void init() throws Exception {
            System.out.println("Person initializing....");
            System.out.println("Person name : "+getPersonName());
            System.out.println("Country : "+getCountry());
        }
    
        @PreDestroy
        public void destroy() throws Exception {
            System.out.println("Bean cleanup....");
        } 
    }
    

    Main.java

    package com.evon.example.callbackMethodExample;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    public class Main {
        public static void main(String[] args) {
            ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/java/com/evon/example/callbackWithAnnotation/bean.xml");
            Person person = (Person) context.getBean("personBean");
            ((FileSystemXmlApplicationContext)context).close(); 
        }
    }
    

    Output :

    Person initializing....
    Person name : Tom
    Country : USA
    Bean cleanup....
    

 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: