SpringCtxUtils.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.cyksj.common.util;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.springframework.beans.BeansException;
  4. import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.ApplicationContextAware;
  7. import org.springframework.stereotype.Component;
  8. /**
  9. * @ClassName SpringCtxUtils
  10. * @Author chan
  11. * @Date 2020-04-27 16:19
  12. * @Description
  13. */
  14. @Component
  15. public final class SpringCtxUtils implements ApplicationContextAware {
  16. private static ApplicationContext context;
  17. @Override
  18. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  19. SpringCtxUtils.context = applicationContext;
  20. }
  21. public static ApplicationContext getContext() {
  22. return context;
  23. }
  24. /**
  25. * 获取对象
  26. *
  27. * @param beanIdOrName bean name
  28. * @return Object 一个以所给名字注册的bean的实例
  29. * @throws BeansException 异常
  30. */
  31. @SuppressWarnings("unchecked")
  32. public static <T> T getBean(String beanIdOrName) throws BeansException {
  33. return StringUtils.isEmpty(beanIdOrName) ? null : (T) context.getBean(beanIdOrName);
  34. }
  35. /**
  36. * 获取类型为requiredType的对象
  37. *
  38. * @param clazz {@link Class}
  39. * @return T
  40. * @throws BeansException 异常
  41. */
  42. public static <T> T getBean(Class<T> clazz) throws BeansException {
  43. return clazz == null ? null : (T) context.getBean(clazz);
  44. }
  45. /**
  46. * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
  47. *
  48. * @param name bean name
  49. * @return boolean
  50. */
  51. public static boolean containsBean(String name) {
  52. return context.containsBean(name);
  53. }
  54. /**
  55. * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
  56. *
  57. * @param name bean name
  58. * @return boolean
  59. * @throws NoSuchBeanDefinitionException 异常
  60. */
  61. public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
  62. return context.isSingleton(name);
  63. }
  64. /**
  65. * @param name bean name
  66. * @return Class 注册对象的类型
  67. * @throws NoSuchBeanDefinitionException 异常
  68. */
  69. public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
  70. return context.getType(name);
  71. }
  72. /**
  73. * 如果给定的bean名字在bean定义中有别名,则返回这些别名
  74. *
  75. * @param name bean name
  76. * @return String array
  77. * @throws NoSuchBeanDefinitionException 异常
  78. */
  79. public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
  80. return context.getAliases(name);
  81. }
  82. /**
  83. * <pre>
  84. * 静默获取bean,无视异常
  85. * </pre>
  86. *
  87. * @param clazz
  88. * @param <T>
  89. * @return
  90. */
  91. public static <T> T getBeanQuietly(Class<T> clazz) {
  92. try {
  93. return clazz == null ? null : (T) context.getBean(clazz);
  94. } catch (Exception e) {
  95. return null;
  96. }
  97. }
  98. }