Pre-initialization and destruction of beans through Spring @PostConstruct and @PreDestroy methods

There are three ways to define the operations performed before the spring container initializes and destroys the bean:
the first: through the @PostConstruct and @PreDestroy methods to achieve the operation performed before the initialization and destruction of the bean
The second is: by defining init-
The third method is to implement the InitializingBean and DisposableBean interfaces through beans.

The following demonstrates through @PostConstruct and @PreDestory
1: Define related implementation classes:
package com.myapp.core.annotation.init;  
  
import javax.annotation.PostConstruct;  
import javax.annotation.PreDestroy;  
  
public class PersonService {  
    
    private String  message;  
  
    public String getMessage() {  
        return message;  
    }  
  
    public void setMessage(String message) {  
        this.message = message;  
    }  
      
    @PostConstruct  
    public void  init(){  
        System.out.println("I'm  init  method  using  @PostConstrut...."+message);  
    }  
      
    @PreDestroy  
    public void  dostory(){  
        System.out.println("I'm  destory method  using  @PreDestroy....."+message);  
    }  
      
}  

2: Define the relevant configuration files:
<?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.1.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.1.xsd">  
  
<context:annotation-config />  
  
<bean id="personService" class="com.myapp.core.annotation.init.PersonService">  
  <property name="message" value="123"></property>  
</bean>  
  
</beans>  

Where <context:annotation-config /> tells the spring container to use annotation configuration: scan annotation configuration
Test class:
package com.myapp.core.annotation.init;  
  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
public class MainTest {  
      
    public static void main(String[] args) {  
          
        ApplicationContext  context = new ClassPathXmlApplicationContext("resource/annotation.xml");  
          
        PersonService   personService  =  (PersonService)context.getBean("personService");  
          
        personService.dostory();  
    }  
  
}  



Among them, you can also tell the Spring container the commonly used annotation configuration method by declaring to load the org.springframework.context.annotation.CommonAnnotationBeanPostProcessor class:
just modify the configuration file to:
<?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.1.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.1.xsd">  
  
<!-- <context:annotation-config /> -->  
  
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />  
<bean id="personService" class="com.myapp.core.annotation.init.PersonService">  
          <property name="message" value="123"></property>  
</bean>
</beans>  


http://blog.csdn.net/topwqp/article/details/8681497

Spring loads bean instantiation orderhttp
://www.cnblogs.com/fanguangdexiaoyuer/p/5886050.html

Bean is in the process of instantiation:
Constructor > @PostConstruct > InitializingBean > init-method
http://sexycoding.iteye.com/blog/1046993

Bean initialization instance in Spring [important]
http://uule.iteye.com/blog/2094609

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326324122&siteId=291194637