From 45dadbefdd0e65768c374d02832d7581b653f509 Mon Sep 17 00:00:00 2001 From: monkeyk7 Date: Sat, 6 Jul 2019 21:42:20 +0800 Subject: [PATCH] Add SOSContextHolder --- .../sos/config/WebSecurityConfigurer.java | 11 +++ .../sos/web/context/SOSContextHolder.java | 82 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/main/java/com/monkeyk/sos/web/context/SOSContextHolder.java diff --git a/src/main/java/com/monkeyk/sos/config/WebSecurityConfigurer.java b/src/main/java/com/monkeyk/sos/config/WebSecurityConfigurer.java index f8f10b8..f1c83af 100644 --- a/src/main/java/com/monkeyk/sos/config/WebSecurityConfigurer.java +++ b/src/main/java/com/monkeyk/sos/config/WebSecurityConfigurer.java @@ -1,6 +1,7 @@ package com.monkeyk.sos.config; import com.monkeyk.sos.service.UserService; +import com.monkeyk.sos.web.context.SOSContextHolder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -97,4 +98,14 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { } + /** + * SOSContextHolder bean + * + * @return SOSContextHolder bean + * @since 2.0.1 + */ + @Bean + public SOSContextHolder sosContextHolder() { + return new SOSContextHolder(); + } } diff --git a/src/main/java/com/monkeyk/sos/web/context/SOSContextHolder.java b/src/main/java/com/monkeyk/sos/web/context/SOSContextHolder.java new file mode 100644 index 0000000..062c57d --- /dev/null +++ b/src/main/java/com/monkeyk/sos/web/context/SOSContextHolder.java @@ -0,0 +1,82 @@ +package com.monkeyk.sos.web.context; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.util.Assert; + +/** + * 2019/7/6 + *

+ *

+ * Spring ApplicationContext Holder, + * get Spring bean from ApplicationContext + * + * @author Shengzhao Li + * @since 2.0.1 + */ +public class SOSContextHolder implements ApplicationContextAware, InitializingBean { + + + private static ApplicationContext applicationContext; + + + 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; + } + + + /** + * Get Spring Bean by clazz. + * + * @param clazz Class + * @param class type + * @return Bean instance + */ + public static T getBean(Class clazz) { + if (applicationContext == null) { + throw new IllegalStateException("applicationContext is null"); + } + return applicationContext.getBean(clazz); + } + + + /** + * Get Spring Bean by beanId. + * + * @param beanId beanId + * @param class type + * @return Bean instance + */ + @SuppressWarnings("unchecked") + public static T getBean(String beanId) { + if (applicationContext == null) { + throw new IllegalStateException("applicationContext is null"); + } + return (T) applicationContext.getBean(beanId); + } + + + @Override + public void afterPropertiesSet() throws Exception { + Assert.notNull(applicationContext, "applicationContext is null"); + } +}