SpringUtils.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package com.poyee.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.springframework.aop.framework.AopContext;
  5. import org.springframework.beans.BeansException;
  6. import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  7. import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
  8. import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
  9. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
  10. import org.springframework.context.ApplicationContext;
  11. import org.springframework.context.ApplicationContextAware;
  12. import org.springframework.stereotype.Component;
  13. import java.lang.reflect.Method;
  14. import java.util.Objects;
  15. /**
  16. * spring工具类 方便在非spring管理环境中获取bean
  17. *
  18. * @author zheng
  19. */
  20. @Slf4j
  21. @Component
  22. public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware {
  23. /**
  24. * Spring应用上下文环境
  25. */
  26. private static ConfigurableListableBeanFactory beanFactory;
  27. private static ApplicationContext applicationContext;
  28. /**
  29. * 获取对象
  30. *
  31. * @param name
  32. * @return Object 一个以所给名字注册的bean的实例
  33. * @throws BeansException
  34. */
  35. @SuppressWarnings("unchecked")
  36. public static <T> T getBean(String name) throws BeansException {
  37. return (T) beanFactory.getBean(name);
  38. }
  39. /**
  40. * 获取类型为requiredType的对象
  41. *
  42. * @param clz
  43. * @return
  44. * @throws BeansException
  45. */
  46. public static <T> T getBean(Class<T> clz) throws BeansException {
  47. T result = beanFactory.getBean(clz);
  48. return result;
  49. }
  50. /**
  51. * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
  52. *
  53. * @param name
  54. * @return boolean
  55. */
  56. public static boolean containsBean(String name) {
  57. return beanFactory.containsBean(name);
  58. }
  59. /**
  60. * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
  61. *
  62. * @param name
  63. * @return boolean
  64. * @throws NoSuchBeanDefinitionException
  65. */
  66. public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
  67. return beanFactory.isSingleton(name);
  68. }
  69. /**
  70. * @param name
  71. * @return Class 注册对象的类型
  72. * @throws NoSuchBeanDefinitionException
  73. */
  74. public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
  75. return beanFactory.getType(name);
  76. }
  77. /**
  78. * 如果给定的bean名字在bean定义中有别名,则返回这些别名
  79. *
  80. * @param name
  81. * @return
  82. * @throws NoSuchBeanDefinitionException
  83. */
  84. public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
  85. return beanFactory.getAliases(name);
  86. }
  87. /**
  88. * 获取aop代理对象
  89. *
  90. * @param invoker
  91. * @return
  92. */
  93. @SuppressWarnings("unchecked")
  94. public static <T> T getAopProxy(T invoker) {
  95. return (T) AopContext.currentProxy();
  96. }
  97. /**
  98. * 获取当前的环境配置,无配置返回null
  99. *
  100. * @return 当前的环境配置
  101. */
  102. public static String[] getActiveProfiles() {
  103. return applicationContext.getEnvironment().getActiveProfiles();
  104. }
  105. /**
  106. * 获取当前的环境配置,当有多个环境配置时,只获取第一个
  107. *
  108. * @return 当前的环境配置
  109. */
  110. public static String getActiveProfile() {
  111. final String[] activeProfiles = getActiveProfiles();
  112. return StringUtils.isNoneBlank(activeProfiles) ? activeProfiles[0] : null;
  113. }
  114. public static AutowireCapableBeanFactory getAutowireCapableBeanFactory() {
  115. return applicationContext.getAutowireCapableBeanFactory();
  116. }
  117. @Override
  118. public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
  119. SpringUtils.beanFactory = beanFactory;
  120. }
  121. @Override
  122. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  123. SpringUtils.applicationContext = applicationContext;
  124. }
  125. /**
  126. * 获取redis
  127. */
  128. public static Object getRedis(String key,String type){
  129. Object obj = null;
  130. try{
  131. Object redisUtils = SpringUtils.getBean("redisUtils");
  132. Method method = null;
  133. switch (type.toLowerCase()) {
  134. case "set":
  135. method = redisUtils.getClass().getDeclaredMethod("sGet", String.class);
  136. break;
  137. case "zset":
  138. method = redisUtils.getClass().getDeclaredMethod("zSGet", String.class);
  139. break;
  140. case "hash":
  141. method = redisUtils.getClass().getDeclaredMethod("hmget", String.class);
  142. break;
  143. case "string":
  144. method = redisUtils.getClass().getDeclaredMethod("get", String.class);
  145. break;
  146. default:
  147. }
  148. if(Objects.nonNull(method)) {
  149. obj = method.invoke(redisUtils, key);
  150. }
  151. }catch (Exception e){
  152. e.printStackTrace();
  153. log.error("获取redis失败");
  154. }
  155. return obj;
  156. }
  157. }