All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dahua.eco.base.spring.AppContextHelper Maven / Gradle / Ivy

package com.dahua.eco.base.spring;

import com.dahua.eco.base.spring.context.loader.AppContextHelperCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Conditional(AppContextHelperCondition.class)
@Order(0)
@Component
public class AppContextHelper implements ApplicationContextAware {
    private static Logger logger = LoggerFactory.getLogger(AppContextHelper.class);
    private static ApplicationContext context = null;
    private static Boolean serviceSatrtCompleted = false;

    public AppContextHelper() {
    }

    public void setApplicationContext(ApplicationContext context) throws BeansException {
        if (AppContextHelper.context == null) {
            setContext(context, false);
            logger.info("Spring init successfully");
        }

    }

    public static  T getBean(String name) {
        if (context == null) {
            throw new IllegalStateException("applicaitonContext inject failure,please restart the service");
        } else {
            try {
                return (T) context.getBean(name);
            } catch (BeansException var2) {
                logger.error(var2.getMessage(), var2);
                return null;
            }
        }
    }

    public static  T getBean(Class clazz) {
        if (context == null) {
            throw new IllegalStateException("applicaitonContext inject failure,please restart the service");
        } else {
            try {
                return context.getBean(clazz);
            } catch (BeansException var2) {
                logger.error(var2.getMessage(), var2);
                return null;
            }
        }
    }

    public static  void setBean(Class clazz, String beanName) {
        if (!context.containsBean(beanName)) {
            BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
            registerBean(beanName, beanDefinitionBuilder.getRawBeanDefinition());
        }

    }

    public static boolean isStartCompleted() {
        return serviceSatrtCompleted;
    }

    public static void setServiceSatrtCompleted(Boolean serviceSatrtCompleted) {
        AppContextHelper.serviceSatrtCompleted = serviceSatrtCompleted;
    }

    public static ApplicationContext getContext() {
        if (context == null) {
            throw new IllegalStateException("applicaitonContext inject failure,please restart the service");
        } else {
            return context;
        }
    }

    public static void setContext(ApplicationContext context, boolean isStartCompleted) {
        if (context != null) {
            AppContextHelper.context = context;
            serviceSatrtCompleted = serviceSatrtCompleted ? serviceSatrtCompleted : isStartCompleted;
            logger.info("Set ApplicationContext successfully");
        }

    }

    private static void registerBean(String beanName, BeanDefinition beanDefinition) {
        ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext)context;
        BeanDefinitionRegistry beanDefinitonRegistry = (BeanDefinitionRegistry)configurableApplicationContext.getBeanFactory();
        beanDefinitonRegistry.registerBeanDefinition(beanName, beanDefinition);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy