`

取WebApplicationContext方法集

 
阅读更多
通过xml配置文件

      bean配置在xml里面,spring提供多种方式读取配置文件得到ApplicationContext.

第一种方式:FileSystemXmlApplicationContext

通过程序在初始化的时候,导入Bean配置文件,然后得到Bean实例:

ApplicationContext ac = new FileSystemXmlApplication Context("applicationContext.xml")
ac.getBean("beanName");


第二种方式:WebApplicationContextUtil

在B/S系统中,通常在web.xml初始化bean的配置文件,然后由WebAppliCationContextUti l得到ApplicationContext.例如:

ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);


其中 servletContext sc 可以具体 换成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext();

另外,由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 对象:

WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);



getRequiredWebApplicatio nContext和 getWebApplicationContext 的区别是,获取失败时候,getRequiredWebApplicatio nContext抛出异常,getWebApplicationContext 抛出null


方法三:继承自抽象类ApplicationObjectSupport

说明:可以方便的获取到ApplicationContext。Spring初始化时,会通过该抽象类的 setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入

方法四:继承自抽象类WebApplicationObjectSupport 说明:类似上面方法,调用getWebApplicationContext ()获取WebApplicationContext

方法五:实现接口ApplicationContextAware 说明:实现该接口的setApplicationContext(ApplicationContext context)方法,
并保存ApplicationContext 对象。Spring初始化时,会通过该方法将ApplicationContext 对象注入
分享到:
评论

相关推荐

    Spring获取webapplicationcontext,applicationcontext几种方法详解

    Spring获取webapplicationcontext,applicationcontext几种方法详解

    在web容器(WebApplicationContext)中获取spring中的bean

    Spring把Bean放在这个容器中,普通的类在需要的时候,直接用getBean()方法取出

    SpringMVC中的RootApplicationContext上下文和WebApplicationContext上下文,通过注解配置SpringMVC的完整解决方案

    注解配置SpringMVC原理简述1. 准备知识1.1 两个应用上下文1.2 ServletContext配置方法(Configuration Methods)1.3 运行时插拔1.4 SpringServletContainerInitializer1.4.1 AbstractContextLoaderInitializer1.4.2 ...

    Spring MVC之WebApplicationContext_动力节点Java学院整理

    主要介绍了Spring MVC之WebApplicationContext的相关资料,需要的朋友可以参考下

    Spring TestContext

    Spring TestContext Framework是spring-test模块中提供的一个测试框架,能够用于单元测试和集成测试。

    在action以外的地方获取dao

    ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(); 或者 ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(); 这是在action以外的地方拿...

    Spring MVC开发配置文件 applicationContext

    Spring Web MVC开发 xml配置文件格式,无bean之类 Spring Web MVC开发配置文件 applicationContext

    spring源代码解析

    代码解析2,部分摘抄 简单的说,在web容器中,通过ServletContext为Spring的IOC容器提供宿主环境,对应的...任何需要访问该ApplicationContext的应用程序代码都可以从WebApplicationContextUtils类的静态方法来得到:

    构建支持Ajax的轻量级J2EE框架

    本文是在Spring+Struts构建的轻量级框架基础上,通过让Buffalo用Spring放在ServletContext里面的 WebApplicationContext(ContextLoaderPlugIn实现把WebApplicationContext的实例放在了 ServletContext中来实现了一...

    ssh-整合资料

    可以通过 WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext()); 来获取

    Spring.html

    WebApplicationContext:web环境使用的容器 注解 创建对象 Component:不分层的注解 Controller:web层 Service:service层 Repository:dao层 管理对象 注入 AutoWired Qualifier Resource ...

    Struts2+Spring3+MyBatis3完整实例

    - Refreshing Root WebApplicationContext: startup date [Tue Jun 14 11:08:28 CST 2011]; root of context hierarchy - Loading XML bean definitions from file [E:\Eclipse_C\workspace\MyStrutsTest\WebRoot\...

    spring+hibernate+osworkflow

    ApplicationContext cxt = WebApplicationContextUtils.getWebApplicationContext(this.getServletConfig().getServletContext()); Workflow wf = (Workflow)cxt.getBean("workflow"); 用osworkflow自带的designer把...

    spring-web-5.3.6 jar包.rar

    这个jar文件包含Web应用开发时,用到Spring框架时所需的核心类, 包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 spring的核心类,提供了核心HTTP...

    ssh(structs,spring,hibernate)框架中的上传下载

    这是Hibernate3引入的新特性,对于包含重量级大数据的表字段,这种抽取方式提高了对大字段操作的灵活性,否则加载Tfile对象的结果集时如果总是返回fileContent,这种批量的数据抽取将可以引起数据库的"洪泛效应"。...

    Spring的注入在Servlet中使用

    Spring的注入在Servlet中使用:在Servlet中使用Spring注入的信息,需要WebApplicationContext这个专门为Web准备的应用上下文

    Spring面试题含答案.pdf

    1. 什么是 spring? 2. 使用 Spring 框架的好处是什么? 3. Spring 由哪些模块组成?...66. WebApplicationContext 67. 什么是 Spring MVC 框架的控制器? 68. @Controller 注解 69. @RequestMapping 注解

    第14章:网络编程(day20).zip

    [[03 08:58:22,466 INFO ] org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:511) - Refreshing WebApplicationContext for namespace 'hessian-...

    spring+springmvc+mybatis的整合

    WebApplicationContext acc = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()); ls = (LoginService) acc.getBean("MyService"); 而讲义上的实现: [java] view ...

    spring-simple-web:使用 Spring Framework 的简单 Web (WAR) 项目

    这个简单的例子演示了在 web 应用程序中使用的 Spring 容器,即org.springframework.web.context.WebApplicationContext Web 应用程序使用 Spring Web 侦听器初始化,例如web.xml org.springframework.web.context...

Global site tag (gtag.js) - Google Analytics