常用的5种获取spring 中bean的方式总结:

方法一:在初始化时保存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。

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

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

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

虽然,spring提供了后三种方法可以实现在普通的类中继承或实现相应的类或接口来获取spring 的ApplicationContext对象,但是在使用是一定要注意实现了这些类或接口的普通java类一定要在Spring 的配置文件application-context.xml文件中进行配置。否则获取的ApplicationContext对象将为null。

如下是我实现了ApplicationContextAware接口的例子

package quartz.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringConfigTool implements ApplicationContextAware{//extends ApplicationObjectSupport{
 
 private static ApplicationContext context = null;
 private static SpringConfigTool stools = null;
 public synchronized static SpringConfigTool init(){
  if(stools == null){
   stools = new SpringConfigTool();
  }
  return stools;
 }
 
 public void setApplicationContext(ApplicationContext applicationContext)
 throws BeansException {
  context = applicationContext;
 }

public synchronized static Object getBean(String beanName) {
  return context.getBean(beanName);
 }

}

XML文件中的配置信息

<bean id="SpringConfigTool" class="quartz.util.SpringConfigTool"></bean>

最后提供一种不依赖于servlet,不需要注入的方式
注意一点,在服务器启动时,Spring容器初始化时,不能通过以下方法获取Spring 容器,如需细节可以观看源码org.springframework.web.context.ContextLoader

Title

1 import org.springframework.web.context.ContextLoader;  
2 import org.springframework.web.context.WebApplicationContext;  
3   
4     WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();  
5     wac.getBean(beanID);  

spring中获取applicationContext的更多相关文章

  1. spring中获取applicationContext(2)

    前几天写web项目的时候,用到了spring mvc. 但是又写bean.我要在代码里面生成,而这个bean里面,又有一些属性是通过spring注入的. 所以,只能通过ApplicationConte ...

  2. spring中获取ApplicationContext对象的技巧,含源码说明

    第一步,实现接口ApplicationContextAware,重写setApplicationContext方法,下方代码标红的地方,绿色部分 可以通过声明来进行存储到本类中. @Component ...

  3. spring mvc在Controller中获取ApplicationContext

    spring mvc在Controller中获取ApplicationContext web.xml中进行正常的beans.xml和spring-mvc.xml的配置: 需要在beans.xml中进行 ...

  4. spring中获取dao或对象中方法的实例化对象

    spring中获取dao的中方法的实例化对象: //获取应用上下文对象 ApplicationContext ctx = new ClassPathXmlApplicationContext(&quo ...

  5. Spring中获取被代理的对象

    目录 Spring中获取被代理的对象 获取Spring被代理对象什么时候可能会用到? Spring中获取被代理的对象 Spring中获取被代理的对象 ### 获取Spring被代理对象的JAVA工具类 ...

  6. spring中获取当前项目的真实路径

    总结: 方法1: WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext ...

  7. spring 代码中获取ApplicationContext(@AutoWired,ApplicationListener)

    2017年度全网原创IT博主评选活动投票:http://www.itbang.me/goVote/234    学习spring框架时间不长,一点一滴都得亲力亲为.今天忽然觉得老是通过@Autowir ...

  8. Spring中获取对象

    Spring是一个非常主流,而且是好用的框架.提供管理对象的容器,提供事务的支持,缓存,权限认证(往往不用).用来集成hibernate等.而管理对象的生命周期是其中一个非常重要的功能.在Spring ...

  9. Spring +quartz获取ApplicationContext上下文

    job存在数据库中,能够进行动态的增增删改查,近期遇到了怎样获取ApplicationContext上下文的问题.解决的方法例如以下 applicationContext-quartz.xml < ...

随机推荐

  1. [leetcode] 19. Count and Say

    这个还是一开始没读懂题目,题目如下: The count-and-say sequence is the sequence of integers beginning as follows: 1, 1 ...

  2. [leetcode] 18. Length of Last Word

    这个题目很简单,给一个字符串,然后返回最后一个单词的长度就行.题目如下: Given a string s consists of upper/lower-case alphabets and emp ...

  3. 笔记本U盘安装CentOS 7

    1. 下载镜像,制作U盘安装盘,设置BIOS启动等内容网上有大量的文章,本文不再赘述. 2. 开机U盘启动后会看到这样的界面: 3. 笔记本安装CentOS最容易出问题的地方在于USB安装盘的选择,如 ...

  4. SQL Server OS 调度

    --SQL SERVER OS 采用合作模式的线程调度模式,即除非Worker主动放弃CPU,否则SQL OS 不会强制剥夺其CPU,从而减少Context Switch --默认设置下,SQL SE ...

  5. Cocos2d-三维拾取Ray-AABB碰撞检测算法【转】

    1.三维拾取技术 在3D游戏中通常会有这样的需求,用户可以选取3D世界中的某些物体进行如拖拽等操作,这时便需要程序通过将二维屏幕上的点坐标转换为三维世界中的坐标,并进行比对,这个过程就需要用到三维拾取 ...

  6. update from用法

    from:https://www.cnblogs.com/zerocc/archive/2011/11/01/2231841.html update 表名 SET  更新字段 FROM 更新表名(多个 ...

  7. c#开发sqlite

    教程:https://www.cnblogs.com/icebutterfly/p/7850689.html 下载sqlite-netFx40-setup-bundle-x64-2010-1.0.96 ...

  8. 我所理解的网络游戏<一>:网游的顶层设计

    网游的基本结构 各大模块的基本功能如下 · 服务器端 登陆服:处理新建玩家.登陆逻辑. 场景服:处理场景服中的逻辑. 中心服:处理跨服的逻辑,实现不同场景服进程的数据调度,以及向数据库查询数据. 数据 ...

  9. Day 1. 占位符的使用方法(%d,%s)(格式化输出)

    方法1  name = input("请输入名字1") age = input ("请输入年龄") hobby = input("请输入兴趣爱好&qu ...

  10. python 简单搭建非阻塞式单进程,select模式,epoll模式服务

    由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 :  --> 点击这里 可以看我的上篇文章 <python 简单搭建阻塞式单进程,多进程, ...