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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM