SpringContextHelper.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2009 by primedata Corporation.
  3. * ,
  4. *
  5. * All rights reserved.
  6. *
  7. * This software is the confidential and proprietary information of
  8. * primedata Corporation ("Confidential Information"). You
  9. * shall not disclose such Confidential Information and shall use
  10. * it only in accordance with the terms of the license agreement
  11. * you entered into with primedata.
  12. */
  13. package com.primeton.dgs.kernel.core.common;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.beans.BeansException;
  16. import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  17. import org.springframework.beans.factory.config.BeanDefinition;
  18. import org.springframework.beans.factory.support.BeanDefinitionBuilder;
  19. import org.springframework.beans.factory.support.BeanDefinitionRegistry;
  20. import org.springframework.context.ApplicationContext;
  21. import org.springframework.context.ApplicationContextAware;
  22. import org.springframework.context.ConfigurableApplicationContext;
  23. /**
  24. * Spring上下文帮助类,方便获取bean、message等
  25. *
  26. * @author user
  27. * @version 1.0 Date: 2009-7-7
  28. */
  29. @Slf4j
  30. public class SpringContextHelper implements ApplicationContextAware {
  31. /**
  32. * Spring上下文
  33. */
  34. private static ApplicationContext applicationContext;
  35. /**
  36. * 获取Spring上下文
  37. *
  38. * @return Spring Application Context
  39. */
  40. public static ApplicationContext getSpringContext() {
  41. return applicationContext;
  42. }
  43. /*
  44. * (non-Javadoc)
  45. *
  46. * @see org.springframework.context.ApplicationContextAware
  47. * #setApplicationContext(org.springframework.context.ApplicationContext)
  48. */
  49. public void setApplicationContext(ApplicationContext applicationContext)
  50. throws BeansException {
  51. SpringContextHelper.applicationContext = applicationContext;
  52. }
  53. /**
  54. * 获取定义在Spring配置文件里的bean实例
  55. *
  56. * @param beanName Spring配置文件里的bean注册名
  57. * @return bean实例(没有找到返回null)
  58. */
  59. public static <T> T getBean(String beanName) {
  60. return (T) applicationContext.getBean(beanName);
  61. }
  62. /**
  63. * 获取类型为requiredType的对象<br>
  64. * 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException)
  65. *
  66. * @param beanName Spring配置文件里的bean注册名
  67. * @param requiredType 返回对象类型
  68. * @return bean实例(没有找到返回null)
  69. */
  70. public static <T> T getBean(String beanName, Class<T> requiredType) throws BeansException {
  71. return applicationContext.getBean(beanName, requiredType);
  72. }
  73. public static <T> T getBean(Class<T> requiredType) throws BeansException {
  74. return applicationContext.getBean(requiredType);
  75. }
  76. public static <T> T getBean(String name, Object... args) throws BeansException {
  77. return (T) applicationContext.getBean(name, args);
  78. }
  79. public static <T> T getBean(Class<T> requiredType, Object... args) throws BeansException {
  80. return applicationContext.getBean(requiredType, args);
  81. }
  82. /**
  83. * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
  84. *
  85. * @param name Spring配置文件里的bean注册名
  86. * @return boolean
  87. */
  88. public static boolean containsBean(String name) {
  89. return applicationContext.containsBean(name);
  90. }
  91. /**
  92. * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
  93. * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
  94. *
  95. * @param name Spring配置文件里的bean注册名
  96. * @return boolean 是否单例
  97. * @throws NoSuchBeanDefinitionException 没有定义Bean
  98. */
  99. public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
  100. return applicationContext.isSingleton(name);
  101. }
  102. /**
  103. * 如果给定的bean名字在bean定义中有别名,则返回这些别名
  104. *
  105. * @param name Spring配置文件里的bean注册名
  106. * @return 别名
  107. * @throws NoSuchBeanDefinitionException 没有定义Bean
  108. */
  109. public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
  110. return applicationContext.getAliases(name);
  111. }
  112. /**
  113. * 手动注入Spring Bean中
  114. *
  115. * @param name BeanName
  116. * @param clazz 注册的bean的类性
  117. * @param args 构造方法的必要参数,顺序和类型要求和clazz中定义的一致
  118. * @param <T>
  119. * @return 返回注册到容器中的bean对象
  120. */
  121. public static <T> T registerBean(String name, Class<T> clazz, Object... args) {
  122. ConfigurableApplicationContext context = null;
  123. if (applicationContext instanceof ConfigurableApplicationContext) {
  124. context = ((ConfigurableApplicationContext) applicationContext);
  125. }
  126. log.info("Register {} to spring context...",clazz.getCanonicalName());
  127. if (context != null) {
  128. if (context.containsBean(name)) {
  129. Object bean = context.getBean(name);
  130. if (bean.getClass().isAssignableFrom(clazz)) {
  131. return (T) bean;
  132. } else {
  133. throw new RuntimeException("BeanName 重复 " + name);
  134. }
  135. }
  136. BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
  137. for (Object arg : args) {
  138. beanDefinitionBuilder.addConstructorArgValue(arg);
  139. }
  140. BeanDefinition beanDefinition = beanDefinitionBuilder.getRawBeanDefinition();
  141. BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) context.getBeanFactory();
  142. beanFactory.registerBeanDefinition(name, beanDefinition);
  143. return context.getBean(name, clazz);
  144. }
  145. return null;
  146. }
  147. }