`
shyboy0358
  • 浏览: 33832 次
文章分类
社区版块
存档分类
最新评论

spring获取ApplicationContext对象的方法

 
阅读更多
方法一:在初始化时保存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对象,然后在通过它获取需要的类实例。

分享到:
评论

相关推荐

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

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

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

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

    获取Spring容器

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

    Spring.html

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

    JSP 获取Spring 注入对象示例

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

    Util通用工具类(轮子类)

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

    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-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 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中文帮助文档

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

    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 持久层 ...

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

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

     此外lazy="true"说明地返回整个Tfile对象时,并不返回fileContent这个字段的数据,只有在显式调用tfile.getFileContent()方法时才真正从数据库中获取fileContent的数据。这是Hibernate3引入的新特性,对于包含重量...

    java8源码-Spring5:Spring5新特性

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

    Spring Bean 的生命周期.docx

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

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

    当事件被发布时,所有注册的监听器将会接收到一个 ApplicationEvent 对象,通过该对象的 getSource() 方法可以获取到事件源,即触发事件的组件。同时,通过 getTimestamp() 方法可以获取到事件发生的时间戳。 ...

    程序间耦合.docx

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

    springmybatis

    其实还有更简单的方法,而且是更好的方法,使用合理描述参数和SQL语句返回值的接口(比如IUserOperation.class),这样现在就可以至此那个更简单,更安全的代码,没有容易发生的字符串文字和转换的错误.下面是详细...

    第五讲spring配置文件.rar

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

Global site tag (gtag.js) - Google Analytics