f1d0d68f76fb02874448e9675539d7062c57572e.svn-base 4.4 KB

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