加載spring核心配置文件引出的問題
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
假設(shè)放在一個會產(chǎn)生多實例的方法里吟榴,多次的new對象营曼,效率很低蛾派。
解決思想:
把加載配置文件和創(chuàng)建對象過程,在服務(wù)器啟動時完成往毡。
實現(xiàn)原理:
- ServletContext對象
- 監(jiān)聽器
- 使用方法
- 在服務(wù)器啟動時候,為每個項目創(chuàng)建一個ServletContext對象
- 在ServletContext對象創(chuàng)建時候靶溜,使用監(jiān)聽器可以具體到ServletContext 對象在什么時候創(chuàng)建
- 使用監(jiān)聽器監(jiān)聽到ServletContext對象創(chuàng)建時候
- 加載spring配置文件开瞭,把配置文件配置對象創(chuàng)建
- 把創(chuàng)建出來的對象放到ServletContext域?qū)ο罄锩?setAttribute方法)
- 獲取對象時候,到ServletContext域得到(getAttribute方法)