SpringContextUtils.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * Copyright (c) 2016-2019 人人开源 All rights reserved.
  3. *
  4. * https://www.renren.io
  5. *
  6. * 版权所有,侵权必究!
  7. */
  8. package io.renren.common.utils;
  9. import org.springframework.beans.BeansException;
  10. import org.springframework.context.ApplicationContext;
  11. import org.springframework.context.ApplicationContextAware;
  12. import org.springframework.stereotype.Component;
  13. /**
  14. * Spring Context 工具类
  15. *
  16. * @author Mark sunlightcs@gmail.com
  17. */
  18. @Component
  19. public class SpringContextUtils implements ApplicationContextAware {
  20. public static ApplicationContext applicationContext;
  21. @Override
  22. public void setApplicationContext(ApplicationContext applicationContext)
  23. throws BeansException {
  24. SpringContextUtils.applicationContext = applicationContext;
  25. }
  26. public static Object getBean(String name) {
  27. return applicationContext.getBean(name);
  28. }
  29. public static <T> T getBean(String name, Class<T> requiredType) {
  30. return applicationContext.getBean(name, requiredType);
  31. }
  32. public static boolean containsBean(String name) {
  33. return applicationContext.containsBean(name);
  34. }
  35. public static boolean isSingleton(String name) {
  36. return applicationContext.isSingleton(name);
  37. }
  38. public static Class<? extends Object> getType(String name) {
  39. return applicationContext.getType(name);
  40. }
  41. }