SpringUtil.java 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package org.zhongzheng.common.utils;
  2. //
  3. // Source code recreated from a .class file by IntelliJ IDEA
  4. // (powered by FernFlower decompiler)
  5. //
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.beans.BeansException;
  9. import org.springframework.context.ApplicationContext;
  10. import org.springframework.context.ApplicationContextAware;
  11. import org.springframework.context.ApplicationEvent;
  12. public class SpringUtil implements ApplicationContextAware {
  13. private static final Logger log = LoggerFactory.getLogger(SpringUtil.class);
  14. private static ApplicationContext context;
  15. public SpringUtil() {
  16. }
  17. public void setApplicationContext(ApplicationContext context) throws BeansException {
  18. SpringUtil.context = context;
  19. }
  20. public static <T> T getBean(Class<T> clazz) {
  21. return clazz == null ? null : context.getBean(clazz);
  22. }
  23. public static <T> T getBean(String beanId) {
  24. return beanId == null ? null : (T)context.getBean(beanId);
  25. }
  26. public static <T> T getBean(String beanName, Class<T> clazz) {
  27. if (null != beanName && !"".equals(beanName.trim())) {
  28. return clazz == null ? null : context.getBean(beanName, clazz);
  29. } else {
  30. return null;
  31. }
  32. }
  33. public static ApplicationContext getContext() {
  34. return context == null ? null : context;
  35. }
  36. public static void publishEvent(ApplicationEvent event) {
  37. if (context != null) {
  38. try {
  39. context.publishEvent(event);
  40. } catch (Exception var2) {
  41. log.error(var2.getMessage());
  42. }
  43. }
  44. }
  45. }