获取Spring的ApplicationContext的方法
在网上搜了一下,写一下我试用的两个方法。
1
2
ApplicationContext ctx=new FileSystemXmlApplicationContext("/application.xml");
//此处的application.xml必须位于系统中一个具体的位置
这个方法貌似只能加载一个XML文件,但是对象内参数的bean配置在其他的xml文件里,所以这个不可以,会报出找不到bean配置的错误。
1
2
ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:spring/*.xml");
//此处的文件必须位于classpath路径中
这个方法可以加载多个XML文件,但如果项目中多个地方都需要用到的话,不太推荐。
下面一种方法适合在多个地方都用到的情况下,建立一个Application的持有类,实现ApplicationContextAware接口。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class ApplicationContextHolder implements ApplicationContextAware{
private static Log log = LogFactory.getLog(ApplicationContextHolder.class);
private static ApplicationContext applicationContext;
@SuppressWarnings("all")
public void setApplicationContext(ApplicationContext context) throws BeansException {
if(this.applicationContext != null) {
throw new IllegalStateException("ApplicationContextHolder already holded 'applicationContext'.");
}
this.applicationContext = context;
log.info("holded applicationContext,displayName:"+applicationContext.getDisplayName());
}
public static ApplicationContext getApplicationContext() {
if(applicationContext == null)
throw new IllegalStateException("'applicationContext' property is null,ApplicationContextHolder not yet init.");
return applicationContext;
}
public static Object getBean(String beanName) {
return getApplicationContext().getBean(beanName);
}
public static void cleanHolder() {
applicationContext = null;
}
}
用法是将bean配置到ApplicationContext.xml文件中.这样保证了一个运行环境下只有一个实例,并且也间接保证了只有一个ApplicationContext对象.其他类中如要获取示例,调用
1
ApplicationContextHolder.getBean("beanName")
就可以了.
还需要把ApplicationContextHolder以bean的形式映射到applicationContext.xml中
获取Spring的ApplicationContext的方法的更多相关文章
- 获取spring的ApplicationContext几种方式【转】
转自:http://blog.sina.com.cn/s/blog_9c7ba64d0101evar.html Java类获取spring 容器的bean 常用的5种获取spring 中bean的方式 ...
- 普通Java类获取Spring的Bean的方法
普通Java类获取Spring的Bean的方法 在SSH集成的前提下.某些情况我们需要在Action以外的类中来获得Spring所管理的Service对象. 之前我在网上找了好几好久都没有找到合适的方 ...
- 怎么获取Spring的ApplicationContext
在 WEB 开发中,可能会非常少须要显示的获得 ApplicationContext 来得到由 Spring 进行管理的某些 Bean, 今天我就遇到了,在这里和大家分享一下, WEB 开发中,怎么获 ...
- 获取spring上下文 - applicationContext
前言 spring上下文是spring容器抽象的一种实现.将你需spring帮你管理的对象放入容器的一种对象,ApplicationContext是一维护Bean定义以及对象之间协作关第的高级接口. ...
- 普通java类获取spring容器bean的方法
很多时候,我们在普通的java类中需要获取spring的bean来做操作,比如,在线程中,我们需要操作数据库,直接通过spring的bean中构建的service就可以完成.无需自己写链接..有时候有 ...
- 手动获取spring的ApplicationContext和bean对象
WEB项目: 方法1: 1 ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(S ...
- 获取Spring的ApplicationContext的几种方式
Application Context定义 简单来说就是Spring中的高级容器,可以获取容器中的各种bean组件,注册监听事件,加载资源文件等功能. 具体定义可以参考官网:https://sprin ...
- 普通Java类获取spring 容器的bean的5种方法
方法一:在初始化时保存ApplicationContext对象方法二:通过Spring提供的工具类获取ApplicationContext对象方法三:继承自抽象类ApplicationObjectSu ...
- 【Spring学习笔记-3.1】让bean获取spring容器上下文(applicationContext.xml)
*.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...
随机推荐
- android之数据库SQLite(一)
创建数据库 首先定义SQLiteOpenHelper的子类 代码如下: package com.example.myandroid; import android.content.Context; i ...
- 使用SimpleXML应该注意的问题有哪些?
SimpleXML提供了一套简单快速的XML操作方法,大大地提高了XML操作的效率. 但是有时不小心也会带来不小的麻烦,看下面一段代码: $xml=simplexml_load_string('< ...
- charles 常用设置
一.过滤网络请求 通常情况下,我们需要对网络请求进行过滤,只监控向指定目录服务器上发送的请求.对于这种需求,我们有2种办法. 1.在主界面的中部的Filter栏中填入需要过滤出来的关键字.例如我们的服 ...
- PHP IDE phpstorm 常用快捷键
PHP IDE phpstorm 常用快捷键 投稿:junjie 字体:[增加 减小] 类型:转载 这篇文章主要介绍了PHP IDE phpstorm 常用快捷键,本文分别列出了mac系统和Win ...
- Ecshop的购物流程代码分析详细说明
Ecshop的购物流程代码分析详细说明 (2012-07-30 10:41:12) 转载▼ 标签: 购物车 结算中心 商品价格 ecshop ecshop购物流程 杂谈 分类: ECSHOP研究院 同 ...
- w win cmd VS broswer
php -f Fetch data from db , use php without any bugs to analyse the data and read and write db, with ...
- Translation Lookaside Buffer
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION In principle, then, e ...
- the OS maintains a number of queues
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION To do its job, the OS ...
- Calculate its MTBF assuming 2000 FITS for each DRAM
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION A common unit of meas ...
- Strong AI Versus Weak AI
Computer Science An Overview _J. Glenn Brookshear _11th Edition The conjecture that machines can be ...