久久96国产精品久久久-久久发布国产伦子伦精品-久久精品国产精品青草-久久天天躁夜夜躁狠狠85麻豆

技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費(fèi)軟件下載基地!

當(dāng)前位置:主頁 > 教程 > 服務(wù)器類 >

JSP 獲取spring容器中bean的兩種方

來源:技術(shù)員聯(lián)盟┆發(fā)布時(shí)間:2017-09-03 06:08┆點(diǎn)擊:

JSP 獲取spring容器中bean的方法總結(jié)

方案1(Web中使用):

ApplicationContext ct = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext()); logService = (ISysLogService) ct.getBean("sysLogServiceImpl");

說明:getRequiredWebApplicationContext方法中需要傳入ServletContext()對(duì)象,在struts2中可以這樣獲取:

ServletActionContext.getServletContext()

方案2(通過手動(dòng)啟動(dòng)spring容器后獲取,如果在web中,不建議這樣使用):

//匹配多個(gè)文件

ApplicationContext ct = new FileSystemXmlApplicationContext(new String[] { "classpath:applicationContext.xml","classpath:lb/*/applicationContext-*.xml"});

//加載一個(gè)文件

ApplicationContext ct = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");

注:獲取spring容器中bean的方法還有幾個(gè),這里就不一一列舉了,個(gè)人認(rèn)為這兩個(gè)是最直接最簡(jiǎn)單的。