国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

幾個(gè)applicationcontext實(shí)現(xiàn)類

2019-11-14 10:44:28
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

ClassPathxmlapplicationContext:類路徑加載 FileSystemXmlApplicationContext:文件系統(tǒng)路徑加載 AnnotationConfigApplicationContext:用于基于注解的配置 WebApplicationContext:專門為web應(yīng)用準(zhǔn)備的,從相對(duì)于Web根目錄的路徑中裝載配置文件完成初始化。

ApplicationContext ac = new ClassPathXmlApplicationContext("com/zzm/context/beans.xml");//等同路徑:"classpath:com/zzm/context/beans.xml"ac.getBean("abc",abc.calss);//就可以獲得bean了ApplicationContext ac = new FileSystemXmlApplicationContext("com/zzm/context/beans.xml");//等同路徑:"file:com/zzm/context/beans.xml"ac.getBean("abc",abc.calss);//就可以獲得bean了

加載多個(gè)配置文件:

ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"conf/beans1.xml","conf.beans2.xml"});

對(duì)于基于注解的,如:

@Conigurationpublic class Beans{ @Bean(name="man") public Man newMan(){ Man man = new Man(); man.setName("小明"); }}

ApplicationContext ac = new AnnotationConfigApplicationContext(Beans.class);Man man = ac.getBean("man",Man.class);

WebApplicationContext初始化需要ServletContext事例,即必須先有Web容器才能完成啟動(dòng)工作,可在web.xml中配置自啟動(dòng)Servlet或定義Web容器監(jiān)聽器(ServletContextListener)。

通過(guò)Web容器監(jiān)聽器:

web.xml:<!--指定配置文件--><context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/zzm-service.xml</param-value> <!--也可以類路徑:classpath:com/zzm/service.xml 可指定多個(gè)用,隔開--></context-param><!--聲明Web容器監(jiān)聽--><listener> <listener-class>org.sPRingframework.web.context.ContextLoaderListener</listener-class></listener>

通過(guò)自啟動(dòng)的Servlet引導(dǎo)

<!--指定配置文件--><context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/zzm-service.xml</param-value></context-param><!--聲明自啟動(dòng)的Servlet容器><servlet> <servlet-name>sprinfContextLoaderServlet</servlet-name> <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> <load-on-startup>1</load-on-startup></servlet>

WebApplicationContext Spring提供WebApplicationContextUtils通過(guò)該類的getWebApplicationContext(ServletContext sc)方法獲取,即可從ServletContext中獲取WebApplicationContext。 新增3作用域:request,session,global session 像Spring中用過(guò)濾器Filter,如過(guò)濾器中需要加載配置時(shí)可用WebApplicationContext來(lái)加載

public class ACLFilter implements Filter{private ServletContext sc;private ApplicationContext ctx;private UserService userService;/** * 過(guò)濾器初始化 */public void init(FilterConfig cfg) throws ServletException { sc= cfg.getServletContext();//獲取Spring容器 ctx=WebApplicationContextUtils.getWebApplicationContext(sc);//從容器中獲取 UserService 對(duì)象 userService=ctx.getBean("userService",UserService.class);}public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain)throws IOException, ServletException { /**/ chain.doFilter(request, response); }public void destroy() {}

}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 连云港市| 昭觉县| 德州市| 万荣县| 南城县| 靖宇县| 新化县| 平遥县| 镇原县| 牙克石市| 来宾市| 龙南县| 峨山| 调兵山市| 天长市| 曲阜市| 高清| 廊坊市| 哈尔滨市| 昭觉县| 浦江县| 汤原县| 临安市| 黔西县| 仙桃市| 白山市| 龙州县| 岳普湖县| 毕节市| 晋州市| 年辖:市辖区| 双辽市| 团风县| 叶城县| 嘉定区| 齐齐哈尔市| 孝感市| 车险| 始兴县| 文水县| 南澳县|