content: package com.cn.xmf.util;
  
  import org.springframework.beans.BeansException;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.ApplicationContextAware;
  
  /**
   * 普通类调用Spring bean对象：
   *
   * @author quyuanchao
   * @date 2018-8-16 18:29
   * <p>Title:com.cn.xmf.utils</p>
   * <p>Description:$DESCRIPTION</p>
   */
  @SuppressWarnings("all")
  public class SpringUtil implements ApplicationContextAware {
  
      private static ApplicationContext applicationContext = null;
  
      @Override
      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
          if (SpringUtil.applicationContext == null) {
              SpringUtil.applicationContext = applicationContext;
          }
      }
  
      /**
       * 获取applicationContext
       */
      public static ApplicationContext getApplicationContext() {
          return applicationContext;
      }
  
      /**
       * 通过name获取 Bean.
       *
       * @param name 全类名
       * @return the bean
       */
      public static Object getBean(String name) {
          return getApplicationContext().getBean(name);
      }
  
      /**
       * 通过class获取Bean.
       *
       * @param <T>   the type parameter
       * @param clazz 类名.class
       * @return the bean
       */
      public static <T> T getBean(Class<T> clazz) {
          return getApplicationContext().getBean(clazz);
      }
  
      /**
       * 通过name，以及class返回指定的Bean
       *
       * @param <T>   the type parameter
       * @param name  the name
       * @param clazz the clazz
       * @return the bean
       */
      public static <T> T getBean(String name, Class<T> clazz) {
          return getApplicationContext().getBean(name, clazz);
      }
  }
  