content: package com.vua.common.util;
  
  import org.springframework.beans.BeansException;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.ApplicationContextAware;
  
  import java.util.Map;
  
  /**
   * Created by liunian on 6/23/17.
   */
  public class SpringContextUtil implements ApplicationContextAware {
  
      private static ApplicationContext context = null;
  
      private SpringContextUtil() {
          super();
      }
  
      @Override
      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
          context = applicationContext;
      }
  
      public static Object getBean(String name) {
          return context.getBean(name);
      }
  
      public static <T> T getBean(String name, Class<T> classz) {
          return context.getBean(name, classz);
      }
  
      public static <T> T getBean(Class<T> classz) {
          T t = null;
          Map<String, T> result = context.getBeansOfType(classz);
          for (Map.Entry<String, T> entry : result.entrySet()) {
              t = entry.getValue();
          }
          return t;
      }
  
      /**
       * 是否包含bean
       * @param beanName
       * @return
       */
      public static boolean containsBean(String beanName) {
          return context.containsBean(beanName);
      }
  
      /**
       * 是否是单例
       * @param beanName
       * @return
       */
      public static boolean isSingleton(String beanName) {
          return context.isSingleton(beanName);
      }
  
      /**
       * bean的类型
       * @param beanName
       * @return
       */
      public static Class getType(String beanName) {
          return context.getType(beanName);
      }
  
  }
  