`
Sev7en_jun
  • 浏览: 1212917 次
  • 性别: Icon_minigender_1
  • 来自: 广州
博客专栏
84184fc0-d0b6-3f7f-a3f0-4202acb3caf5
Apache CXF使用s...
浏览量:109892
社区版块
存档分类
最新评论

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

 
阅读更多

FROM: http://www.blogjava.net/Todd/archive/2010/04/22/295112.html 

 

方法一:在初始化时保存ApplicationContext对象
代码:
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");
说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。

方法二:通过Spring提供的工具类获取ApplicationContext对象
代码:
import org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId");
说明:
这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。

上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。

 

其中 servletContext sc 可以具体 换成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext(); 另外,由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出 WebApplicationContext 对象:

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

 

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

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

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


在web应用中一般用ContextLoaderListener加载webapplication,如果需要在action之外或者control类之外获取webapplication思路之一是,单独写个类放在static变量中,
类似于:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->public class AppContext {

  
private static AppContext instance;

  
private AbstractApplicationContext appContext;

  
public synchronized static AppContext getInstance() {
    
if (instance == null) {
      instance 
= new AppContext();
    }
    
return instance;
  }

  
private AppContext() {
    
this.appContext = new ClassPathXmlApplicationContext(
        
"/applicationContext.xml");
  }

  
public AbstractApplicationContext getAppContext() {
    
return appContext;
  }
}

不过这样,还是加载了2次applicationcontext,servlet一次,路径加载一次;觉得不如直接用路径加载,舍掉servlet加载
在网上也找了些其他说法:实现ApplicationContextAware,,, 接口,或者servletcontextAware接口,还要写配置文件。有的竟然要把配置文件里的listener,换成自己的类,这样纯粹多此一举。不过有的应用不是替换,是在补一个listener,
我在一版的jpetstore(具体那一版不知道)里发现了这个:
[web.xml]里
     
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->    <listener>
        
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    
</listener>
    
    
<listener>
        
<listener-class>com.ibatis.jpetstore.util.SpringInit</listener-class>
    
</listener>
其中SpringInit实现接口ServletContextListener

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->package com.ibatis.jpetstore.util;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


public class SpringInit implements ServletContextListener {
    

    
private static WebApplicationContext springContext;
    
    
public SpringInit() {
        
super();
    }
    
    
public void contextInitialized(ServletContextEvent event) {
        springContext 
= WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
    }
    

    
public void contextDestroyed(ServletContextEvent event) {
    }
    
    
public static ApplicationContext getApplicationContext() {
        
return springContext;
    }

    
}

在其中的一个bean的构造里SpringInit获取applicationcontext,代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->  public OrderBean() {
    
this(
            (AccountService) SpringInit.getApplicationContext().getBean(
"accountService"),
            (OrderService) SpringInit.getApplicationContext().getBean(
"orderService") );
  }

恩,这种在action,servlet之外的bean里获取applicationcontext的方法值得参考,应该有用
分享到:
评论

相关推荐

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

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

    Spring MVC开发配置文件 applicationContext

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

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

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

    spring jar 包详解

    spring jar 包详解 spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了 spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到spring-mock.jar来进行辅助测试,正式应用...

    Spring面试题含答案.pdf

    25. 解释 Spring 支持的几种 bean 的作用域 26. Spring 框架中的单例 bean 是线程安全的吗? 27. 解释 Spring 框架中 bean 的生命周期 28. 哪些是重要的 bean 生命周期方法? 你能重载它们吗? 29. 什么是 Spring ...

    spring-web-5.3.6 jar包.rar

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

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

    spring jar 包详解spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了 spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到spring-mock.jar来进行辅助测试,正式应用系统...

    spring4.1核心包

    可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI 所需的全部类,instrumentation组件以及校验Validation 方面的相关类。 外部依赖spring-beans, (spring-aop)。 5. spring-context-support-4.1.1....

    spring源代码解析

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

    开源框架 Spring Gossip

    结合 JSTL &lt;spring:bind&gt; 标签 数据绑定的几个方法 &lt;spring:message&gt; 标签 &lt;spring:transform&gt; 标签 其它 View 层 除了 JSP View 层技术之外,您还可以使用其它的 View 层技术,或建立...

    最新最全的spring开发包

    可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI所需的全部类,UI方面的用来与模板(Templating)引擎如 Velocity、FreeMarker、JasperReports集成的类,以及校验Validation方面的相关类。...

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

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

    spring+springmvc+mybatis的整合

    但是有一些部分自己没有能完成,主要是如何从spring容器里取出ApplicationContext,这个我的实现比较low,看了看讲义,才OK的。 我的实现: [java] view plain copy WebApplicationContext acc = ...

    Struts2+Spring3+MyBatis3完整实例

    - Initializing Spring root WebApplicationContext - Root WebApplicationContext: initialization started - Refreshing Root WebApplicationContext: startup date [Tue Jun 14 11:08:28 CST 2011]; root of ...

    spring-web-2.5.jar

    org.springframework.web.context.WebApplicationContext.class org.springframework.web.context.request.AbstractRequestAttributes.class org.springframework.web.context.request....

    在action以外的地方获取dao

    这是在action以外的地方拿ApplicationContext的方法,需要的参数是:ServletContext,在request.getServletContext里能拿到,所以只要有request就能拿到spring配置文件里的bean. 这种方法通常在写组件时用,比如写...

    spring+hibernate+osworkflow

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

    Spring.html

    Spring IOC 控制反转:把创建对象的权利交给Spring 创建对象 1.无参构造 2.静态工厂 3.实例工厂 管理对象 对象关系DI 构造器注入 set注入 生命周期 scope:prototype/singleton init-...

    Spring中文帮助文档

    3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @...

    Spring API

    3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @...

Global site tag (gtag.js) - Google Analytics