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

com.cybermkd.common.spring.SpringBuilder Maven / Gradle / Ivy

package com.cybermkd.common.spring;

import com.cybermkd.common.util.Stringer;
import com.cybermkd.log.Logger;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ConfigurableApplicationContext;

import static com.cybermkd.common.util.Checker.checkNotNull;

/**
 * @author Dreampie
 * @date 2015-10-08
 * @what
 */
public class SpringBuilder {

    private final static Logger logger = Logger.getLogger(SpringBuilder.class);

    private static ConfigurableApplicationContext context;

    public static ConfigurableApplicationContext getContext() {
        return SpringBuilder.context;
    }

    public static void setContext(ConfigurableApplicationContext context) {
        checkNotNull(context, "Could not found context for spring.");
        SpringBuilder.context = context;
        SpringHolder.alive = true;
    }

    public static void refreshContext() {
        if (SpringHolder.alive) {
            SpringBuilder.context.refresh();
        }
    }

    public static void removeContext() {
        if (SpringHolder.alive) {
            SpringBuilder.context.close();
            SpringBuilder.context = null;
            SpringHolder.alive = false;
        }
    }

    /**
     * 注册bean
     *
     * @param clazz
     */
    public static void register(Class clazz) {
        ConfigurableApplicationContext context = getContext();
        if (context != null) {
            DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory();
            String beanName = Stringer.firstLowerCase(clazz.getSimpleName());
            beanFactory.registerBeanDefinition(beanName, BeanDefinitionBuilder.rootBeanDefinition(clazz).getBeanDefinition());
        }
    }

    public static void registerSingleton(Class clazz) {
        try {
            registerSingleton(clazz, clazz.newInstance());
        } catch (InstantiationException e) {
            logger.error(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            logger.error(e.getMessage(), e);
        }
    }

    public static void registerSingleton(Class clazz, Object bean) {
        ConfigurableApplicationContext context = getContext();
        if (context != null) {
            DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory();
            String beanName = Stringer.firstLowerCase(clazz.getSimpleName());
            beanFactory.registerSingleton(beanName, bean);
        }
    }

    public static  T getBean(Class clazz) {
        ConfigurableApplicationContext context = getContext();
        if (context != null) {
            return context.getBean(clazz);
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy