摘自: http://blog.csdn.net/yang123111/article/details/32099329

获取Spring的上下文环境ApplicationContext的方式

Web项目中发现有人如此获得Spring的上下环境:

public class SpringUtil {

   public static ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

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

}

在web项目中这种方式非常不可取!!!

分析:

首先,主要意图就是获得Spring上下文;

其次,有了Spring上下文,希望通过getBean()方法获得Spring管理的Bean的对象;

最后,为了方便调用,把上下文定义为static变量或者getBean方法定义为static方法;

但是,在web项目中,系统一旦启动,web服务器会初始化Spring的上下文的,我们可以很优雅的获得Spring的ApplicationContext对象。

如果使用

new ClassPathXmlApplicationContext("applicationContext.xml");

相当于重新初始化一遍!!!!

也就是说,重复做启动时候的初始化工作,第一次执行该类的时候会非常耗时!!!!!

正确的做法是:

@Component

public class SpringContextUtil implements ApplicationContextAware {

     private static ApplicationContext applicationContext; // Spring应用上下文环境

     /*

      * 实现了ApplicationContextAware 接口,必须实现该方法;

      *通过传递applicationContext参数初始化成员变量applicationContext

      */

     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
} public static ApplicationContext getApplicationContext() {
return applicationContext;
} @SuppressWarnings("unchecked")
public static <T> T getBean(String name) throws BeansException {
return (T) applicationContext.getBean(name);
}

}

注意:这个地方使用了Spring的注解@Component,如果不是使用annotation的方式,而是使用xml的方式管理Bean,记得写入配置文件

其实

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

这种方式获取Sping上下文环境,最主要是在测试环境中使用,比如写一个测试类,系统不启动的情况下手动初始化Spring上下文再获取对象!

获取Spring的上下文环境ApplicationContext的方式的更多相关文章

  1. 十二种获取Spring的上下文环境ApplicationContext的方法

    转载:https://my.oschina.net/u/2391658/blog/729414

  2. Java代码获取spring 容器的bean几种方式

    一.目的 写了一个项目,多个module,然后想在A模块中实现固定的config注入,当B模块引用A时候,能够直接填写相对应的配置信息就行了.但是遇到一个问题,B引用A时候,A的配置信息总是填充不了, ...

  3. 获取spring容器上下文(webApplicationContext)的几种方法

    在很多情况,我们需要先获取spring容器上下文,即webApplicationContext,然后通过webApplicationContext来获取其中的bean.典型的情况是,我想在一个并未委托 ...

  4. 怎么随时获取Spring的上下文ApplicaitonContext,和Spring管理的Bean

    BeanFactory接口 Interface BeanFactory getBean <T> T getBean(String name, Class<T> required ...

  5. 【Spring】非Spring IOC容器下获取Spring IOC上下文的环境

    前言 在Spring Web项目中,有些特殊的时候需要在非Spring IOC容器下获取Spring IOC容器的上下文环境,比如获取某个bean. 版本说明 声明POM文件,指定需引入的JAR. & ...

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

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

  7. 获取Spring应用环境上下文bean

    import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBean ...

  8. Spring MVC 上下文(ApplicationContext)初始化入口

    Spring 常用上下文容器有哪些 ApplicationContext ClassPathXmlApplicationContext ApplicationContext context = new ...

  9. 获取spring上下文 - applicationContext

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

随机推荐

  1. [MySQL] 常用SQL的优化--18.4

    这里介绍下,Insert.Group By等SQL语句的优化方法: 1.大批量数据插入 当load命令导入数据的时候,可以进行适当的设置提高导入速度. 1.1 对于MyISAM表,可以先禁用非唯一索引 ...

  2. 【JavaScript】JavaScript模拟Class

    beauty("$Class",["$underscore"],function(_){ var Class = function () { var lengt ...

  3. vacabulary1

    The hard hat is rigid,so nothing will hurt my head. glue 胶水vegetarian 素食者: 素食主义者:素食的 North Korea 朝鲜S ...

  4. position中多次用到了relative和absolute,能不能具体介绍一下这两者的区别?

    position中多次用到了relative和absolute,能不能具体介绍一下这两者的区别? 一个是相对定位,一个是绝对定位. absolute生成绝对定位的元素,相对于 static 定位以外的 ...

  5. bootstrap与jqueryui按钮冲突的解决

    bootstrap与jqueryui按钮冲突的解决 (2013-10-15 14:09:36)转载▼ 标签: 情感 分类: jQuery 参考: http://getbootstrap.com/jav ...

  6. Centos 7配置LAMP

    因为安装zabbix需要LAMP环境,特记录如下. LAMP指的Linux(操作系统).Apache HTTP 服务器,MySQL(有时也指MariaDB,数据库软件)和PHP(有时也是指Perl或P ...

  7. js json与字符串转换

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. LeetCode 125. Valid Palindrome

    这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...

  9. POJ3352 Road Construction (双连通分量)

    Road Construction Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u Sub ...

  10. 项目管理: Maven 让事情变得简单

    http://maven.apache.org/,  Maven其实就是为java实现的一个构建工具.他比Ant更高端. 目前,绝大多数开发人员都把 Ant 当作 Java 编程项目的标准构建工具.遗 ...