package com.bonus.core.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {
   private static final Logger log = LoggerFactory.getLogger(SpringContextHolder.class);
   private static ApplicationContext applicationContext = null;

   public static ApplicationContext getApplicationContext() {
      return applicationContext;
   }

   @Override
   public void setApplicationContext(ApplicationContext applicationContext) {
      SpringContextHolder.applicationContext = applicationContext;
   }

   public static <T> T getBean(String name) {
      return (T) applicationContext.getBean(name);
   }

   public static <T> T getBean(Class<T> requiredType) {
      return applicationContext.getBean(requiredType);
   }

   public static void clearHolder() {
      if (log.isDebugEnabled()) {
         log.debug("清除SpringContextHolder中的ApplicationContext:" + String.valueOf(applicationContext));
      }

      applicationContext = null;
   }

   public static void publishEvent(ApplicationEvent event) {
      if (applicationContext != null) {
         applicationContext.publishEvent(event);
      }
   }

   @Override
   public void destroy() {
      clearHolder();
   }
}
