From 1a36c944fb002e54ce84c38eb026b999e79543a0 Mon Sep 17 00:00:00 2001 From: mkk Date: Tue, 9 Jun 2020 22:37:38 +0800 Subject: [PATCH] Use beanFactory getBean replaced old --- .../sos/web/context/SOSContextHolder.java | 43 +++++++------------ 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/monkeyk/sos/web/context/SOSContextHolder.java b/src/main/java/com/monkeyk/sos/web/context/SOSContextHolder.java index 062c57d..ac03ebe 100644 --- a/src/main/java/com/monkeyk/sos/web/context/SOSContextHolder.java +++ b/src/main/java/com/monkeyk/sos/web/context/SOSContextHolder.java @@ -1,9 +1,9 @@ package com.monkeyk.sos.web.context; import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.InitializingBean; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; import org.springframework.util.Assert; /** @@ -11,36 +11,24 @@ import org.springframework.util.Assert; *

*

* Spring ApplicationContext Holder, - * get Spring bean from ApplicationContext + * get Spring bean from beanFactory * * @author Shengzhao Li * @since 2.0.1 */ -public class SOSContextHolder implements ApplicationContextAware, InitializingBean { +public class SOSContextHolder implements BeanFactoryAware, InitializingBean { - private static ApplicationContext applicationContext; + private static BeanFactory beanFactory; public SOSContextHolder() { } + @Override - public void setApplicationContext(ApplicationContext context) throws BeansException { - SOSContextHolder.applicationContext = context; - } - - - /** - * Retrieves the {@code ApplicationContext} set when Spring created and initialized the holder bean. If the - * holder has not been created (see the class documentation for details on how to wire up the holder), or if - * the holder has not been initialized, this accessor may return {@code null}. - *

- * - * @return the set context, or {@code null} if the holder bean has not been initialized - */ - public static ApplicationContext getApplicationContext() { - return applicationContext; + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + SOSContextHolder.beanFactory = beanFactory; } @@ -52,10 +40,10 @@ public class SOSContextHolder implements ApplicationContextAware, InitializingBe * @return Bean instance */ public static T getBean(Class clazz) { - if (applicationContext == null) { - throw new IllegalStateException("applicationContext is null"); + if (beanFactory == null) { + throw new IllegalStateException("beanFactory is null"); } - return applicationContext.getBean(clazz); + return beanFactory.getBean(clazz); } @@ -68,15 +56,16 @@ public class SOSContextHolder implements ApplicationContextAware, InitializingBe */ @SuppressWarnings("unchecked") public static T getBean(String beanId) { - if (applicationContext == null) { - throw new IllegalStateException("applicationContext is null"); + if (beanFactory == null) { + throw new IllegalStateException("beanFactory is null"); } - return (T) applicationContext.getBean(beanId); + return (T) beanFactory.getBean(beanId); } @Override public void afterPropertiesSet() throws Exception { - Assert.notNull(applicationContext, "applicationContext is null"); + Assert.notNull(beanFactory, "beanFactory is null"); } + }