JSP 獲取spring容器中bean的兩種方法總結
更新時間:2017年05月26日 11:47:06 投稿:lqh
這篇文章主要介紹了JSP 獲取spring容器中bean的方法總結的相關資料,需要的朋友可以參考下
JSP 獲取spring容器中bean的方法總結
方案1(Web中使用):
ApplicationContext ct = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext()); logService = (ISysLogService) ct.getBean("sysLogServiceImpl");
說明:getRequiredWebApplicationContext方法中需要傳入ServletContext()對象,在struts2中可以這樣獲?。?/p>
ServletActionContext.getServletContext()
方案2(通過手動啟動spring容器后獲取,如果在web中,不建議這樣使用):
//匹配多個文件
ApplicationContext ct = new FileSystemXmlApplicationContext(new String[] { "classpath:applicationContext.xml","classpath:lb/*/applicationContext-*.xml"});
//加載一個文件
ApplicationContext ct = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
注:獲取spring容器中bean的方法還有幾個,這里就不一一列舉了,個人認為這兩個是最直接最簡單的。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
- 關于SpringBoot獲取IOC容器中注入的Bean(推薦)
- 詳解Spring簡單容器中的Bean基本加載過程
- 淺談spring容器中bean的初始化
- spring在IoC容器中裝配Bean詳解
- Spring的Bean容器介紹
- Spring為IOC容器注入Bean的五種方式詳解
- spring-spring容器中bean知識點總結
- Spring實戰(zhàn)之讓Bean獲取Spring容器操作示例
- Spring實戰(zhàn)之抽象Bean和子Bean定義與用法示例
- Spring實戰(zhàn)之調用實例工廠方法創(chuàng)建Bean操作示例
- Spring實戰(zhàn)之使用靜態(tài)工廠方法創(chuàng)建Bean操作示例
- Spring實戰(zhàn)之容器中的工程Bean用法示例
相關文章
攜程Apollo(阿波羅)安裝部署以及java整合實現(xiàn)
這篇文章主要介紹了攜程Apollo(阿波羅)安裝部署以及java整合實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08SpringMVC?Restful風格與中文亂碼問題解決方案介紹
Restful就是一個資源定位及資源操作的風格,不是標準也不是協(xié)議,只是一種風格,是對http協(xié)議的詮釋,下面這篇文章主要給大家介紹了關于SpringMVC對Restful風格支持的相關資料,需要的朋友可以參考下2022-10-10