在网上搜了一下,写一下我试用的两个方法。

 

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的方法的更多相关文章

  1. 获取spring的ApplicationContext几种方式【转】

    转自:http://blog.sina.com.cn/s/blog_9c7ba64d0101evar.html Java类获取spring 容器的bean 常用的5种获取spring 中bean的方式 ...

  2. 普通Java类获取Spring的Bean的方法

    普通Java类获取Spring的Bean的方法 在SSH集成的前提下.某些情况我们需要在Action以外的类中来获得Spring所管理的Service对象. 之前我在网上找了好几好久都没有找到合适的方 ...

  3. 怎么获取Spring的ApplicationContext

    在 WEB 开发中,可能会非常少须要显示的获得 ApplicationContext 来得到由 Spring 进行管理的某些 Bean, 今天我就遇到了,在这里和大家分享一下, WEB 开发中,怎么获 ...

  4. 获取spring上下文 - applicationContext

    前言 spring上下文是spring容器抽象的一种实现.将你需spring帮你管理的对象放入容器的一种对象,ApplicationContext是一维护Bean定义以及对象之间协作关第的高级接口. ...

  5. 普通java类获取spring容器bean的方法

    很多时候,我们在普通的java类中需要获取spring的bean来做操作,比如,在线程中,我们需要操作数据库,直接通过spring的bean中构建的service就可以完成.无需自己写链接..有时候有 ...

  6. 手动获取spring的ApplicationContext和bean对象

    WEB项目: 方法1: 1 ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(S ...

  7. 获取Spring的ApplicationContext的几种方式

    Application Context定义 简单来说就是Spring中的高级容器,可以获取容器中的各种bean组件,注册监听事件,加载资源文件等功能. 具体定义可以参考官网:https://sprin ...

  8. 普通Java类获取spring 容器的bean的5种方法

    方法一:在初始化时保存ApplicationContext对象方法二:通过Spring提供的工具类获取ApplicationContext对象方法三:继承自抽象类ApplicationObjectSu ...

  9. 【Spring学习笔记-3.1】让bean获取spring容器上下文(applicationContext.xml)

    *.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...

随机推荐

  1. 字符串中的一些基本操作函数(c语言)

    其中很多函数返回的都是首地址,程序中只是将该地址后的内容全部输出来了...并没有作特殊处理输出地址...还有几个函数有点小bug. #include"iostream" #incl ...

  2. 创建自定义 jQuery 移动主题

    自定义页面.工具栏.内容.表单元素.列表.按钮等元素的外观 智能电话和平板设备的高采用率最终导致增加了对移动 Web 开发人员和设计师的需求.jQuery Mobile 框架支持您创建能与原生应用程序 ...

  3. [转]如何:定义和处理 SOAP 标头

    本文转自:http://msdn.microsoft.com/zh-cn/library/vstudio/8728chd5(v=vs.100).aspx 本主题专门介绍一项旧有技术.现在应通过使用以下 ...

  4. OpenCV学习笔记——视频的边缘检测

    使用Canny算子进行边缘检测,并分开输出到三个窗口中,再给每一个窗口添加文字 代码: #include"cv.h" #include"highgui.h" / ...

  5. The resource could not be loaded because the App Transport Security policy requires the use of a secure connection

    xmpp 项目中遇到的问题,用苹果的通信API 写一个PUT 方法,向服务器上传一张图片.遇到如题问题. Plist 文件没有NSAppTransportSecurity属性 Dic,添加该属性,再添 ...

  6. 安装nfs服务器

    服务器和客户端都有一下操作 groupadd nginx useradd -r -g nginx nginx -s /sbin/nologin id nginx 查看nginx的id yum inst ...

  7. ORA-01791: 不是 SELECTed 表达式

    Oracle 9i数据库,执行下面语句出现错误“ORA-01791: 不是 SELECTed 表达式”:select distinct t.name from auth_employee t orde ...

  8. Apache Spark源码走读之9 -- Spark源码编译

    欢迎转载,转载请注明出处,徽沪一郎. 概要 本来源码编译没有什么可说的,对于java项目来说,只要会点maven或ant的简单命令,依葫芦画瓢,一下子就ok了.但到了Spark上面,事情似乎不这么简单 ...

  9. Javascript 笔记与总结(2-7)对象

    html: <h1>找对象</h1> <div id="div1"> <p>p1</p> <p>p2< ...

  10. linux常用命令的英文单词缩写

    命令缩写: ls:list(列出目录内容) cd:Change Directory(改变目录) su:switch user 切换用户rpm:redhat package manager 红帽子打包管 ...