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

 

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. 【iBoard电子学堂】【iCore双核心板】资料光盘A盘更新,版本号为A6

    _____________________________________ 深入交流QQ群: A: 204255896(1000人超级群,可加入) B: 165201798(500人超级群,满员) C ...

  2. POJ 2777 Count Color(线段树染色,二进制优化)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42940   Accepted: 13011 Des ...

  3. OpenVirteX 安装

    参考 sdnlab 带你走进OpenVirteX之环境搭建 ubuntu14.04安装OpenVirteX 官网链接 系统要求: Recommended Cores GB java heap size ...

  4. Bootstrap页面布局17 - BS选项卡

    代码结构: <div class='container-fluid'> <h2 class='page-header'>Bootstrap 选项卡</h2> < ...

  5. Bootstrap页面布局5 - 响应式布局(格式)

    旨在优化不同上网设备中页面显示的优化 响应式布局:就是根据浏览窗口的尺寸,改变页面的变化 原理:利用css的media-queries判断浏览窗口的尺寸,在CSS样式表中设置一些规则! 例如: 在&l ...

  6. asp.net webform杂记

    context.Response.ContentType = "text/plain";            string encodeFileName = HttpUtilit ...

  7. xml提取

    $url = 'http://221.232.141.108/hsdcw/news.xml'; $opts = array( 'http'=>array( 'method'=>" ...

  8. common.js

    //检测浏览器 function checkb(){ var Sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; (s = ua. ...

  9. 纯C++文件调用MFC类

    在VS2008中 将预编译头属性 由 不使用预编译头 改成 使用使用预编译头 在响应的.cpp文件的最前面 #include "stdafx.h"

  10. CLR调试报错“Visual Studio远程调试监视器 (MSVSMON.EXE) 的 64 位版本无法调试 32 位进程或 32 位转储。请改用 32 位版本”的解决

    Win7 64位电脑上进行visual studio的数据库项目的CLR存储过程进行调试时,报错: ---------------------------Microsoft Visual Studio ...