zoukankan      html  css  js  c++  java
  • Spring的ApplicationContext加载备忘

    最常用的办法就是用 ClassPathXmlApplicationContext, FileSystemClassPathXmlApplicationContext, FileSystemXmlApplicationContext 等对象去加载Spring配置文件,这样做也是可以, 但是在加载Spring配置文件的时候,就会生成一个新的ApplicaitonContext对象而不是Spring容器帮我们生成的哪一个, 这样就产生了冗余, 所以不采用应用程序手动加载文件的方式,而是使用ApplicationContextAware让Spring容器自动传递自己生成的 ApplicationContext到指定的类里。将来可以通过该类方便的访问spring的上下文。

    1web工程使用的ApplicationContextAware类:

    /**
    * 以静态变量保存Spring ApplicationContext,可在任意代码中取出ApplicaitonContext.
    *
    */
    public class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    /**
    * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
    */
    public void setApplicationContext(ApplicationContext applicationContext) {
    SpringContextHolder.applicationContext =applicationContext;
    }


    /**
    * 取得存储在静态变量中的ApplicationContext.
    */
    public static ApplicationContext getApplicationContext() {
    checkApplicationContext();
    return applicationContext;
    }

    /**
    * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
    */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
    checkApplicationContext();
    return (T) applicationContext.getBean(name);
    }

    /**
    * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
    */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
    checkApplicationContext();
    return (T) applicationContext.getBeansOfType(clazz);
    }

    private static void checkApplicationContext() {
    if (applicationContext == null)
    throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextUtil");
    }
    }

    同时需要在xml里定义这个bean,这样Spring在创建完ApplicationContext才会调用setApplicationContext(ApplicationContext applicationContext) 注入到类中,对于一些静态类由于无法使用注入而通过SpringContextHolder可以很方便的访问bean实例。

        <bean class="××××××.SpringContextHolder"     lazy-init="false" />



    2.对于java工程,ApplicationContextAware的意义不是很大,因为普通java工程没有办法象web应用启动的时候,加载spring的上下文。所以,还是应用程序手动创建方便。比如在main方法中通过ClassPathXmlApplicationContext来获取Bean,而不是通过注入,因为注入是实例变量。另外,ClassPathXmlApplicationContext即使使用了注解仍然可以访问到Bean。与使用注解与否无关。

  • 相关阅读:
    二分查找代码
    顺序查找代码
    js原生获取css属性
    前端使用nginx上传文件时,进度获取不对
    动态赋值poster,无法显示
    git 命令收藏
    promise笔记
    vscode自定义代码块
    vuex的初始化
    webstorm添加自定义代码块
  • 原文地址:https://www.cnblogs.com/highriver/p/2424001.html
Copyright © 2011-2022 走看看