|
@@ -0,0 +1,88 @@
|
|
|
|
|
+package com.mrxu.framework.boot.util;
|
|
|
|
|
+
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
|
|
+import org.springframework.context.ApplicationContextAware;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+import org.springframework.web.context.support.WebApplicationContextUtils;
|
|
|
|
|
+
|
|
|
|
|
+import javax.servlet.ServletContextEvent;
|
|
|
|
|
+import javax.servlet.ServletContextListener;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 工具类获取bean对象
|
|
|
|
|
+ */
|
|
|
|
|
+@Component
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class SpringApplicationContextUtil implements ServletContextListener,ApplicationContextAware{
|
|
|
|
|
+
|
|
|
|
|
+ private static ApplicationContext springContext;
|
|
|
|
|
+
|
|
|
|
|
+ public SpringApplicationContextUtil() {
|
|
|
|
|
+ super();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void setApplicationContext(ApplicationContext arg0) throws BeansException {
|
|
|
|
|
+ if(springContext==null) {
|
|
|
|
|
+ springContext = arg0;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void contextDestroyed(ServletContextEvent arg0) {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void contextInitialized(ServletContextEvent event) {
|
|
|
|
|
+ springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static ApplicationContext getApplicationContext() {
|
|
|
|
|
+ return springContext;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String getContextId() {
|
|
|
|
|
+ return springContext.getId();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String getApplicationName() {
|
|
|
|
|
+ String contextId = getContextId();
|
|
|
|
|
+ if(!StringUtils.isEmpty(contextId)) {
|
|
|
|
|
+ String[] splits = contextId.split(":");
|
|
|
|
|
+ if(splits.length>0) {
|
|
|
|
|
+ return splits[0];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static <T> T getBean(Class<T> clazz){
|
|
|
|
|
+ return springContext.getBean(clazz);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ public static <T> T getBean(String name) {
|
|
|
|
|
+ return (T) springContext.getBean(name);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ public static <T> T getBeanByClassName(String className) {
|
|
|
|
|
+ Class<T> clazz = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ clazz = (Class<T>) Class.forName(className);
|
|
|
|
|
+ }catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ log.error("class not fount"+className);
|
|
|
|
|
+ }
|
|
|
|
|
+ return springContext.getBean(clazz);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static <T> T getBean(String string, Class<T> clazz) {
|
|
|
|
|
+ return (T)springContext.getBean(string, clazz);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|