`
xchyou
  • 浏览: 27010 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

获取spring的ApplicationContext对象

阅读更多

方法一:在初始化时保存ApplicationContext对象
new ClassPathXmlApplicationContext("applicationContext.xml").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。
 
方法三:继承自抽象类ApplicationObjectSupport
     抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。


方法四:继承自抽象类WebApplicationObjectSupport
         类似上面方法,调用getWebApplicationContext()获取WebApplicationContext
 
方法五:实现接口ApplicationContextAware
     实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。Spring初始化时,会通过该方法将ApplicationContext 对象注入。
 
总结:系统中用到上述方法的类实际上就于Spring框架紧密耦合在一起了,因为这些类是运行在Spring框架上的,因此,系统中,应该尽量的减少这类应用,使系统尽可能的独立于当前运行环境。
分享到:
评论

相关推荐

    Spring获取ApplicationContext对象工具类的实现方法

    主要介绍了 Spring获取ApplicationContext对象工具类的实现方法的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下

    spring中通过ApplicationContext getBean获取注入对象的方法实例

    今天小编就为大家分享一篇关于spring中通过ApplicationContext getBean获取注入对象的方法实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    获取Spring容器

    在Service端,自定义ApplicationContext,获取Spring容器。从而,通过编码的形式,手动获取注册到容器中的对象。

    JSP 获取Spring 注入对象示例

    @ page import=org.springframework.context.ApplicationContext%> ServletContext sc = this.getServletConfig().getServletContext(); ApplicationContext ac2 = WebApplicationContextUtils.get...

    Spring.html

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

    Spring-Reference_zh_CN(Spring中文参考手册)

    5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 5.4.2.1. 注册用户自定义的PropertyEditor 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 ...

    Spring.3.x企业应用开发实战(完整版).part2

    1.7 如何获取Spring 1.8 小结 第2章 快速入门 2.1 实例功能概述 2.1.1 比Hello World更适用的实例 2.1.2 实例功能简介 2.2 环境准备 2.2.1 创建库表 2.2.2 建立工程 2.2.3 类包及Spring配置文件规划 2.3 持久层 ...

    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 2.0 开发参考手册

    5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @...

    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. @...

    程序间耦合.docx

    获取spring的Ioc核心容器,并根据id获取对象 * * ApplicationContext的三个常用实现类: * ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。...

    Util通用工具类(轮子类)

    避免重复造轮子,开发中常用封装...渲染工具类,资源文件相关的操作类,对比两个对象的变化的工具类,Spring的ApplicationContext的持有者,可以用静态方法的方式获取spring容器中的bean,sql语句工具类,高频方法集合类

    spring chm文档

    5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @...

    Spring3.x企业应用开发实战(完整版) part1

    1.7 如何获取Spring 1.8 小结 第2章 快速入门 2.1 实例功能概述 2.1.1 比Hello World更适用的实例 2.1.2 实例功能简介 2.2 环境准备 2.2.1 创建库表 2.2.2 建立工程 2.2.3 类包及Spring配置文件规划 2.3 持久层 ...

    java8源码-Spring5:Spring5新特性

    加载配置文件时候不会创建对象,获取对象时后才会创建。 ApplicationContext:BeanFactory的子接口,提供更多强大的功能,一般是开发人员使用 加载配置文件时候会创建对象。一般采用这种,将耗时放在启动时候 ...

    Spring Bean 的生命周期.docx

    Spring的生命周期是指实例化Bean时所经历的一系列阶段,即通过getBean()获取bean对象及设置对象属性时,Spring框架做了哪些事。Bean的生命周期从Spring容器实例化Bean到销毁Bean。 本文分别对 BeanFactory 和 ...

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

    WEB-INF下的applicationContext.xml为Spring的配置文件,struts-config.xml为Struts的配置文件,file-upload.jsp为文件上传页面,file-list.jsp为文件列表页面。  本文后面的章节将从数据持久层->业务层->Web层的...

    springmybatis

    mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in ...

    SpringBoot事件发布及订阅详解含代码示例(值得珍藏)

    当某个特定的事件发生时,ApplicationEvent 对象会被创建并发布到 ApplicationContext 中,所有注册监听该事件的监听器就会收到通知并执行相应的操作。 ApplicationEvent 是一个泛型类,可以用来传递任何类型的数据...

    第五讲spring配置文件.rar

    接着上一讲的Java工程。 配置文件详解 alias:为bean设置别名;可以设置多个别名 id是bean的标识符,要...如果不配置id和name,可以根据applicationContext.getBean(Class)获取对象 团队协作可以通过import来完成

Global site tag (gtag.js) - Google Analytics