zoukankan      html  css  js  c++  java
  • 手动获取Spring上下文和Bean对象

    示例

    /**
     * 手动获取Spring上下文和Bean对象
     *
     * @Author YinWenBing
     * @Date 2017/1/6  17:07
     */
    public class ApplicationUtil implements ApplicationContextAware {
    
        private static ApplicationContext applicationContext;
    
        /**
         * 加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware接口,会自动调用该方法
         *
         * @param applicationContext
         * @throws BeansException
         */
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            ApplicationUtil.applicationContext = applicationContext;
        }
    
        /**
         * 获取Spring上下文
         * @return
         */
        public static ApplicationContext getApplicationContext() {
            return applicationContext;
        }
    
        /**
         * 获取Spring Bean
         * @param name
         * @return
         * @throws BeansException
         */
        public static Object getBean(String name) throws BeansException {
            return applicationContext.getBean(name);
        }
    }
    

    在Spring配置文件中手动配置ApplicationUtil Bean

    <bean id="applicationUtil" class="com.nb.soa.common.util.ApplicationUtil"></bean>
    
  • 相关阅读:
    势函数的构造
    10.29模拟赛总结
    10.29vp总结
    10.25模拟赛总结
    10.24模拟赛总结
    线段树练习
    一键挖矿
    P1972 [SDOI2009]HH的项链
    P3901 数列找不同
    P5546 [POI2000]公共串
  • 原文地址:https://www.cnblogs.com/yinwenbing/p/6377579.html
Copyright © 2011-2022 走看看