SpringUtils.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.ruoyi.common.utils.spring;
  2. import com.ruoyi.common.utils.StringUtils;
  3. import org.springframework.aop.framework.AopContext;
  4. import org.springframework.beans.BeansException;
  5. import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  6. import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
  7. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
  8. import org.springframework.stereotype.Component;
  9. /**
  10. * spring工具类 方便在非spring管理环境中获取bean
  11. *
  12. * @author ruoyi
  13. */
  14. @Component
  15. public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware
  16. {
  17. /** Spring应用上下文环境 */
  18. private static ConfigurableListableBeanFactory beanFactory;
  19. private static ApplicationContext applicationContext;
  20. @Override
  21. public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
  22. {
  23. SpringUtils.beanFactory = beanFactory;
  24. }
  25. @Override
  26. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
  27. {
  28. SpringUtils.applicationContext = applicationContext;
  29. }
  30. /**
  31. * 获取对象
  32. *
  33. * @param name
  34. * @return Object 一个以所给名字注册的bean的实例
  35. * @throws org.springframework.beans.BeansException
  36. *
  37. */
  38. @SuppressWarnings("unchecked")
  39. public static <T> T getBean(String name) throws BeansException
  40. {
  41. return (T) beanFactory.getBean(name);
  42. }
  43. /**
  44. * 获取类型为requiredType的对象
  45. *
  46. * @param clz
  47. * @return
  48. * @throws org.springframework.beans.BeansException
  49. *
  50. */
  51. public static <T> T getBean(Class<T> clz) throws BeansException
  52. {
  53. T result = (T) beanFactory.getBean(clz);
  54. return result;
  55. }
  56. /**
  57. * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
  58. *
  59. * @param name
  60. * @return boolean
  61. */
  62. public static boolean containsBean(String name)
  63. {
  64. return beanFactory.containsBean(name);
  65. }
  66. /**
  67. * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
  68. *
  69. * @param name
  70. * @return boolean
  71. * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
  72. *
  73. */
  74. public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException
  75. {
  76. return beanFactory.isSingleton(name);
  77. }
  78. /**
  79. * @param name
  80. * @return Class 注册对象的类型
  81. * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
  82. *
  83. */
  84. public static Class<?> getType(String name) throws NoSuchBeanDefinitionException
  85. {
  86. return beanFactory.getType(name);
  87. }
  88. /**
  89. * 如果给定的bean名字在bean定义中有别名,则返回这些别名
  90. *
  91. * @param name
  92. * @return
  93. * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
  94. *
  95. */
  96. public static String[] getAliases(String name) throws NoSuchBeanDefinitionException
  97. {
  98. return beanFactory.getAliases(name);
  99. }
  100. /**
  101. * 获取aop代理对象
  102. *
  103. * @param invoker
  104. * @return
  105. */
  106. @SuppressWarnings("unchecked")
  107. public static <T> T getAopProxy(T invoker)
  108. {
  109. return (T) AopContext.currentProxy();
  110. }
  111. /**
  112. * 获取当前的环境配置,无配置返回null
  113. *
  114. * @return 当前的环境配置
  115. */
  116. public static String[] getActiveProfiles()
  117. {
  118. return applicationContext.getEnvironment().getActiveProfiles();
  119. }
  120. /**
  121. * 获取当前的环境配置,当有多个环境配置时,只获取第一个
  122. *
  123. * @return 当前的环境配置
  124. */
  125. public static String getActiveProfile()
  126. {
  127. final String[] activeProfiles = getActiveProfiles();
  128. return StringUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null;
  129. }
  130. }