SpringUtils.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.ruoyi.common.utils.spring;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  4. import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
  5. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
  6. import org.springframework.stereotype.Component;
  7. /**
  8. * spring工具类 方便在非spring管理环境中获取bean
  9. *
  10. * @author ruoyi
  11. */
  12. @Component
  13. public final class SpringUtils implements BeanFactoryPostProcessor
  14. {
  15. /** Spring应用上下文环境 */
  16. private static ConfigurableListableBeanFactory beanFactory;
  17. @Override
  18. public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
  19. {
  20. SpringUtils.beanFactory = beanFactory;
  21. }
  22. /**
  23. * 获取对象
  24. *
  25. * @param name
  26. * @return Object 一个以所给名字注册的bean的实例
  27. * @throws org.springframework.beans.BeansException
  28. *
  29. */
  30. @SuppressWarnings("unchecked")
  31. public static <T> T getBean(String name) throws BeansException
  32. {
  33. return (T) beanFactory.getBean(name);
  34. }
  35. /**
  36. * 获取类型为requiredType的对象
  37. *
  38. * @param clz
  39. * @return
  40. * @throws org.springframework.beans.BeansException
  41. *
  42. */
  43. public static <T> T getBean(Class<T> clz) throws BeansException
  44. {
  45. T result = (T) beanFactory.getBean(clz);
  46. return result;
  47. }
  48. /**
  49. * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
  50. *
  51. * @param name
  52. * @return boolean
  53. */
  54. public static boolean containsBean(String name)
  55. {
  56. return beanFactory.containsBean(name);
  57. }
  58. /**
  59. * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
  60. *
  61. * @param name
  62. * @return boolean
  63. * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
  64. *
  65. */
  66. public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException
  67. {
  68. return beanFactory.isSingleton(name);
  69. }
  70. /**
  71. * @param name
  72. * @return Class 注册对象的类型
  73. * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
  74. *
  75. */
  76. public static Class<?> getType(String name) throws NoSuchBeanDefinitionException
  77. {
  78. return beanFactory.getType(name);
  79. }
  80. /**
  81. * 如果给定的bean名字在bean定义中有别名,则返回这些别名
  82. *
  83. * @param name
  84. * @return
  85. * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
  86. *
  87. */
  88. public static String[] getAliases(String name) throws NoSuchBeanDefinitionException
  89. {
  90. return beanFactory.getAliases(name);
  91. }
  92. }