當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
【Spring注解系列08】@PostConstruct与@PreDestroy
生活随笔
收集整理的這篇文章主要介紹了
【Spring注解系列08】@PostConstruct与@PreDestroy
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.@PostConstruct與@PreDestroy
@PostConstruct:在bean創建完成并且屬性賦值完成;來執行初始化方法 @PreDestroy:在容器銷毀bean之前通知我們進行清理工作2.實例
@Service public class PersonService {@PostConstructpublic void post(){System.out.println("PersonService ... PostConstruct");}@PreDestroypublic void pre(){System.out.println("PersonService ... PreDestroy");} }@Configuration @ComponentScan(value = {"com.java.service"}) public class PostConstructConfig { }//測試類 public class PostConstructTest {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(PostConstructConfig.class);System.out.println("applicationContext ..... 初始化結束");//只有bean從容器中移除時,才會調用preDestroy來通知System.out.println("applicationContext ..... 準備關閉");applicationContext.close();System.out.println("applicationContext ..... 已關閉");} }運行結果:PersonService ... PostConstruct applicationContext ..... 初始化結束 applicationContext ..... 準備關閉 PersonService ... PreDestroy applicationContext ..... 已關閉?
總結
以上是生活随笔為你收集整理的【Spring注解系列08】@PostConstruct与@PreDestroy的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Spring注解系列07】Spring
- 下一篇: 【Spring注解系列09】Spring