一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 監聽器獲取Spring配置文件的方法

監聽器獲取Spring配置文件的方法

2020-05-13 14:11eson_15 JAVA教程

這篇文章主要為大家詳細介紹了監聽器獲取Spring配置文件的方法,很實用的方法,感興趣的小伙伴們可以參考一下

我們在做項目的時候,會用到監聽器去獲取Spring的配置文件,然后從中拿出我們需要的bean出來,比如做網站首頁,假設商品的后臺業務邏輯都做好了,我們需要創建一個監聽器,在項目啟動時將首頁的數據查詢出來放到application里,即在監聽器里調用后臺商品業務邏輯的方法,也就是說我們需要在監聽器里獲取Spring中配置的相應的bean。先把監聽器創建出來:

1. 創建InitDataListener
創建一個監聽器InitDataListener繼承ServletContextListener:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * @Description: TODO(用于項目啟動的時候數據初始化)
 * @author eson_15
 *
 */
//@Component //監聽器是web層的組件,它是tomcat實例化的,不是Spring實例化的。不能放到Spring中
public class InitDataListener implements ServletContextListener {
 
 private ProductService productService = null;//productService中定義了跟商品相關的業務邏輯
 
 @Override
 public void contextDestroyed(ServletContextEvent event) {
 
 }
 
 @Override
 public void contextInitialized(ServletContextEvent event) {
 
 }
 
}
 

并在web.xml中配置該監聽器:

監聽器獲取Spring配置文件的方法

如上,productService中定義了商品的一些業務邏輯,并且這個productService是交給Spring管理的,那么我們如何得到這個對象呢?首先肯定的一點是:我們不能自己new出來,因為new出來的話就跟Spring的IoC沒有關系了……主要有三種方式可以實現,我們先一個個分析,最后比較優劣。

2. 直接加載beans.xml文件
這種方式比較簡單粗暴,不是要加載配置文件么?那好,我加載就是了,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//@Component //監聽器是web層的組件,它是tomcat實例化的,不是Spring實例化的。不能放到Spring中
public class InitDataListener implements ServletContextListener {
 
 
 private ProductService productService = null; //productService中定義了跟商品相關的業務邏輯
 
 @Override
 public void contextDestroyed(ServletContextEvent event) {
 
 }
 
 @Override
 public void contextInitialized(ServletContextEvent event) {
 // 獲取業務邏輯類productService查詢商品信息
 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
 productService = (ProductService) context.getBean("productService");
 System.out.println(productService); //輸出看看拿到了沒有
 
 //下面是具體productService相關操作……
 }
 
}
 

這種方法完全沒問題,思路很清晰,先加載配置文件beans.xml,然后獲取bean,但是啟動tomcat后,我們看看控制臺輸出的信息:

監聽器獲取Spring配置文件的方法

到這里應該發現這種方式的弊端了,加載了兩次配置文件,也就是說那些bean被實例化了兩次,從打印的信息來看,是拿到我們自己加載配置文件是實例化的bean。這種方式明顯不可取。

3. 從ServletContext中獲取
從上面的方法中,我們最起碼可以知道,Spring通過自己的監聽器已經加載過一次配置文件了,我們沒必要再加載一次,那么很容易想到,如果知道Spring加載后放到哪里了,那我們就可以從那地方獲取該配置文件,下面我們看下Spring加載配置文件的過程:

監聽器獲取Spring配置文件的方法

上圖中(省略了無關的代碼),ContextLoaderListener就是web.xml中我們配置的Spring監聽器,它也實現了ServletContextListener并繼承了ContextLoader。在監聽器中主要通過initWebApplicationContext方法來獲取配置文件,并創建WebApplicationContext對象,在initWebApplicationContext方法里主要做兩件事:一是拿到Spring的上下文,二是把Spring上下文放到ServletContext中,并且鍵為:WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE。那么如何拿到Spring的上下文呢?是通過獲取web.xml中配置的Spring的路徑,CONFIG_LOCATION_PARM其實是個字符串常量,就是上面web.xml中配置Spring監聽器下面的:

?
1
2
3
4
5
<context-param>
 <param-name>contextConfigLocation</param-name>
 <!--CONFIG_LOCATION_PARM就是contextConfigLocation-->
 <param-value>classpath:beans.xml</param-value>
</context-param>
 

所以就很明顯了,通過web.xml中配置的路徑拿到beans.xml,然后加載這個配置文件,實例化bean。
現在我們既然知道了Spring在加載配置文件后,把它放在了ServletContext中,那么我們就可以去這里面直接拿!

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//@Component //監聽器是web層的組件,它是tomcat實例化的,不是Spring實例化的。不能放到Spring中
public class InitDataListener implements ServletContextListener {
 
 
 private ProductService productService = null;
 
 @Override
 public void contextDestroyed(ServletContextEvent event) {
 // TODO Auto-generated method stub
 
 }
 
 @Override
 public void contextInitialized(ServletContextEvent event) {
 // 獲取業務邏輯類查詢商品信息
 
 // 解決方案二,項目在啟動時,把Spring配置文件通過Spring的監聽器加載,存儲到ServletContext中,我們只要在ServletContext中獲取即可。
 ApplicationContext context = (ApplicationContext) event.getServletContext()
  .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
 productService = (ProductService) context.getBean("productService");
 System.out.println(productService);
 }
 
}
 

這樣我們就可以拿到produceService的實例化對象了,這種方法好是好,就是getAttribute中的參數太長,也不知道當時程序員的腦門子被夾了還是咋地,估計是想不到其他更合適的名字了吧~

4. 通過Spring提供的工具類加載
也許開發Spring的大牛們也意識到了這個參數名字太長了,于是他們提供了一個方法類,可以加載配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class InitDataListener implements ServletContextListener {
 
 
 private ProductService productService = null;
 
 @Override
 public void contextDestroyed(ServletContextEvent event) {
 // TODO Auto-generated method stub
 
 }
 
 @Override
 public void contextInitialized(ServletContextEvent event) {
 // 獲取業務邏輯類查詢商品信息
 
 WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
 productService = (ProductService) context.getBean("productService");
 System.out.println(productService);
 }
 
}
 

其實,這里的getWebApplicationContext方法就是把上面的那個方法封裝了一下而已,我們看看這個方法的源碼就知道了:

?
1
2
3
public static WebApplicationContext getWebApplicationContext(ServletContext sc) {
 return getWebApplicationContext(sc, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
 }
 

        這樣更加方便程序員調用,僅此而已……所以一般我們使用第三種方法來獲取Spring的配置文件,從而獲取相應的實例化bean。

原文鏈接:http://blog.csdn.net/eson_15/article/details/51373937

以上就是iPhone6splus微信閃退的解決方法,希望對大家有所幫助,也希望大家多多支持服務器之家,關注服務器之家的更多精彩內容。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品久久久麻豆国产精品 | 亚洲精品午夜久久aaa级久久久 | 四虎影视库永久在线地址 | 精品老司机在线视频香蕉 | 天干夜天天夜天干天ww | 996热在线视频| 国产精品www | 国产东北3p真实在线456视频 | 韩国三级年轻小的胰子完整 | 6969精品视频在线观看 | 午夜宅男在线观看 | 青春草在线观看视频 | 色婷婷久久综合中文久久一本` | 免费人成在线观看 | 亚洲H成年动漫在线观看不卡 | 国产亚洲精品线观看77 | 亚洲国产视频网站 | 三级全黄裸体 | julianann办公室 | 99久久国产亚洲综合精品 | 久久电影院久久国产 | 调教女帝| 99国内精品久久久久久久黑人 | 黄网在线观看免费网站台湾swag | 桥本有菜ssni-677在线观看 | 色戒完整版2小时38分钟 | 日本三级免费观看 | 扒开斗罗美女了的胸罩和内裤漫画 | 亚洲国产成人精品激情 | 亚洲冬月枫中文字幕在线看 | 国产麻豆视频 | 99在线精品免费视频 | 日韩综合久久 | 四虎国产精品视频免费看 | 四虎影院网址大全 | 日本国产成人精品视频 | 99在线观看视频免费 | 精品国产一区二区 | 青草久久网 | 护士被多人调教到失禁h | 欧美乱码视频 |