SpringUtils.java 4.4 KB

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