zoukankan      html  css  js  c++  java
  • 提供静态方法获取spring bean实例

    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    
    /**
     * 提供静态方法获取spring bean实例
     * 
     * @author
     *
     */
    @Component
    public class SpringContextUtils implements ApplicationContextAware {
    
        //将相关服务注入Spring框架
        @Autowired
        private ImportValidateService _importValidateService;
            
        @Autowired
        private CallBackService _callBackService;
        
        private static ApplicationContext _applicationContext = null;
    
        private static SpringContextUtils _self;
    
        public static <T> T getBean(Class<T> requiredType) {
            if (_applicationContext != null) {
                return _applicationContext.getBean(requiredType);
            } else {
                throw new org.springframework.context.ApplicationContextException("ApplicationContext has not been set.");
            }
        }
    
        public synchronized static ImportValidateService getValidateService() {
            return self()._importValidateService;
        }
    
        
        public synchronized static CallBackService getCallBackService() {
            return self()._callBackService;
        }
    
        private static SpringContextUtils self() {
            if (_self == null) {
                if (_applicationContext != null) {
                    _self = _applicationContext.getBean(SpringContextUtils.class);
                } else {
                    throw new org.springframework.context.ApplicationContextException(
                            "ApplicationContext has not been set.");
                }
            }
            return _self;
        }
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            _applicationContext = applicationContext;
        }
    
    }
  • 相关阅读:
    Mysql 用户管理
    php插件名称 yum安装
    U盘模式无法引导进入pe系统
    修改 ssh 远程连接 时间
    tomcat 安装在 linux
    tomcat 配置文件 server.xml
    Linux 安装 jdk
    高可用web架构: LVS+keepalived+nginx+apache+php+eaccelerator(+nfs可选 可不选)
    Keepalived 工作原理和配置说明
    Mysql 初始化 及 密码管理
  • 原文地址:https://www.cnblogs.com/liangblog/p/13180830.html
Copyright © 2011-2022 走看看