美文网首页
Spring获取上下文ApplicationContext方式

Spring获取上下文ApplicationContext方式

作者: 蜗牛写java | 来源:发表于2019-03-21 00:43 被阅读0次

    Spring获取上下文ApplicationContext方式

    启动类中获取

    public class ApplicationContextUtil {
    
        private static ApplicationContext applicationContext = null;
    
        public static void setApplicationContext(ApplicationContext applicationContext) {
            if (SpringApplicationContextUtil.applicationContext == null) {
                SpringApplicationContextUtil.applicationContext = applicationContext;
            }
    
        }
    
        //获取applicationContext
        public static ApplicationContext getApplicationContext() {
            return applicationContext;
        }
    
        //通过name获取 Bean
        public static Object getBean(String name) {
            if (applicationContext == null) return null;
            return getApplicationContext().getBean(name);
    
        }
    
        //通过class获取Bean
        public static Object T getBean(Class<?> requiredType) {
            if (applicationContext == null) return null;
            return getApplicationContext().getBean(clazz);
        }
    
        //通过name,以及Clazz返回指定的Bean
        public static Object T getBean(String name, Class<?> clazz) {
            if (applicationContext == null) return null;
            return getApplicationContext().getBean(name, clazz);
        }
    
    }
    
    public static void main(String[] args) {
        ApplicationContext run = SpringApplication.run(MusicCpApplication.class, args);
        ApplicationContextUtil.setApplicationContext(run);
    }
    

    继承ApplicationContextAware

    @Component
    public class ApplicationContextUtil implements ApplicationContextAware {
        private static ApplicationContext applicationContext = null;
    
        //取得存储在静态变量中的ApplicationContext
        public static ApplicationContext getApplicationContext() {
            return applicationContext;
        }
    
        //从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型
        public static Object getBean(String name) {
            return applicationContext.getBean(name);
        }
    
        //从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型
        public static Object getBean(Class<?> requiredType) {
            return applicationContext.getBean(requiredType);
        }
    
        //该方法为重点;主要是利用Spring后置处理器(BeanPostProcessor)触发该操作;
        //实现ApplicationContextAware接口, 注入Context到静态变量中
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) {
            .ApplicationContextUtil.applicationContext = applicationContext;
        }
    
    }
    

    源码分析:ApplicationContextAwareProcessor

    class ApplicationContextAwareProcessor implements BeanPostProcessor {
    
        private final ConfigurableApplicationContext applicationContext;
    
        private final StringValueResolver embeddedValueResolver;
    
    
        /**
         * Create a new ApplicationContextAwareProcessor for the given context.
         */
        public ApplicationContextAwareProcessor(ConfigurableApplicationContext applicationContext) {
            this.applicationContext = applicationContext;
            this.embeddedValueResolver = new EmbeddedValueResolver(applicationContext.getBeanFactory());
        }
    
    
        @Override
        public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
            AccessControlContext acc = null;
    
            //bean instanceof ApplicationContextAware  重点
            if (System.getSecurityManager() != null &&
                    (bean instanceof EnvironmentAware 
                     || bean instanceof EmbeddedValueResolverAware 
                     || bean instanceof ResourceLoaderAware 
                     || bean instanceof ApplicationEventPublisherAware 
                     || bean instanceof MessageSourceAware 
                     || bean instanceof ApplicationContextAware)) {
                acc = this.applicationContext.getBeanFactory().getAccessControlContext();
            }
    
            if (acc != null) {
                AccessController.doPrivileged(new PrivilegedAction<Object>() {
                    @Override
                    public Object run() {
                        invokeAwareInterfaces(bean);
                        return null;
                    }
                }, acc);
            }
            else {
                invokeAwareInterfaces(bean);
            }
    
            return bean;
        }
    
        private void invokeAwareInterfaces(Object bean) {
            if (bean instanceof Aware) {
                if (bean instanceof EnvironmentAware) {
                    ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
                }
                if (bean instanceof EmbeddedValueResolverAware) {
                    ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
                }
                if (bean instanceof ResourceLoaderAware) {
                    ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
                }
                if (bean instanceof ApplicationEventPublisherAware) {
                    ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
                }
                if (bean instanceof MessageSourceAware) {
                    ((MessageSourceAware) bean).setMessageSource(this.applicationContext);
                }
                 //重点
                if (bean instanceof ApplicationContextAware) {
                    ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
                }
            }
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) {
            return bean;
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:Spring获取上下文ApplicationContext方式

          本文链接:https://www.haomeiwen.com/subject/frcfvqtx.html