content: package com.alanpoi.common.util;
  
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.beans.BeansException;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.ApplicationContextAware;
  
  import javax.annotation.PostConstruct;
  import java.net.InetAddress;
  import java.net.UnknownHostException;
  
  /**
   * bean管理器
   *
   * @author zhuoxun.peng
   */
  @Slf4j
  public class ApplicationUtil implements ApplicationContextAware {
  
      private static ApplicationContext applicationContext;
  
      private static InetAddress inetAddress;
  
      @PostConstruct
      private void init() {
          try {
              inetAddress = InetAddress.getLocalHost();
          } catch (UnknownHostException e) {
              log.warn("init server ip address exception:{}", e);
          }
      }
  
      @Override
      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
          if (ApplicationUtil.applicationContext == null) {
              this.applicationContext = applicationContext;
          }
      }
  
      public static ApplicationContext getApplicationContext() {
          return applicationContext;
      }
  
      public static Object getBean(String name) {
          return getApplicationContext().getBean(name);
      }
  
      public static <T> T getBean(Class<T> clazz) {
          try {
              return getApplicationContext().getBean(clazz);
          } catch (Exception e) {
              log.warn("bean not is exist:" + clazz);
              return null;
          }
      }
  
      public static <T> T getBean(String name, Class<T> clazz) {
          return getApplicationContext().getBean(name, clazz);
      }
  
      @Deprecated
      public static InetAddress getInetAddress() {
          return inetAddress;
      }
  }
  