spring的@PostConstruct 和 @PreDestory注解,类似于在配置文件中定义init-method 和 destory-method方法

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);  
    }  
      
}  

然后,在spring.xml中配置bean,最后启动:

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonService personService = (PersonService)context.getBean("personService");
context.destroy();

 

posted @ 2017-03-21 17:19  Orc_Warrior  阅读(4602)  评论(0编辑  收藏  举报