Use beanFactory getBean replaced old

2.1.0
mkk 5 years ago
parent ff7147912a
commit 1a36c944fb

@ -1,9 +1,9 @@
package com.monkeyk.sos.web.context; package com.monkeyk.sos.web.context;
import org.springframework.beans.BeansException; 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.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
@ -11,36 +11,24 @@ import org.springframework.util.Assert;
* <p> * <p>
* <p> * <p>
* Spring ApplicationContext Holder, * Spring ApplicationContext Holder,
* get Spring bean from ApplicationContext * get Spring bean from beanFactory
* *
* @author Shengzhao Li * @author Shengzhao Li
* @since 2.0.1 * @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() { public SOSContextHolder() {
} }
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
SOSContextHolder.applicationContext = context;
}
@Override
/** public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
* Retrieves the {@code ApplicationContext} set when Spring created and initialized the holder bean. If the SOSContextHolder.beanFactory = beanFactory;
* 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}.
* <p/>
*
* @return the set context, or {@code null} if the holder bean has not been initialized
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
} }
@ -52,10 +40,10 @@ public class SOSContextHolder implements ApplicationContextAware, InitializingBe
* @return Bean instance * @return Bean instance
*/ */
public static <T> T getBean(Class<T> clazz) { public static <T> T getBean(Class<T> clazz) {
if (applicationContext == null) { if (beanFactory == null) {
throw new IllegalStateException("applicationContext is 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") @SuppressWarnings("unchecked")
public static <T> T getBean(String beanId) { public static <T> T getBean(String beanId) {
if (applicationContext == null) { if (beanFactory == null) {
throw new IllegalStateException("applicationContext is null"); throw new IllegalStateException("beanFactory is null");
} }
return (T) applicationContext.getBean(beanId); return (T) beanFactory.getBean(beanId);
} }
@Override @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
Assert.notNull(applicationContext, "applicationContext is null"); Assert.notNull(beanFactory, "beanFactory is null");
} }
} }

Loading…
Cancel
Save