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

com.efeichong.util.SpringUtils Maven / Gradle / Ivy

package com.efeichong.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author : lxk
 * @Date : 2018/8/31 11:36
 * @Description : 获取springBean 工具类
 */
@Order(-1)
@Component
public class SpringUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    /**
     * 获取applicationContext
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringUtils.applicationContext == null) {
            SpringUtils.applicationContext = applicationContext;
        }
    }

    public static Object get() {
        return getApplicationContext();
    }

    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static  T getBean(String name) {
        return (T) getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param 
     * @return
     */
    public static  T getBean(Class clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name
     * @param clazz
     * @param 
     * @return
     */
    public static  T getBean(String name, Class clazz) {
        return getApplicationContext().getBean(name, clazz);
    }

    /**
     * 是否为spring运行环境
     *
     * @return
     */
    public static boolean hasSpringRunTime() {
        return applicationContext != null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy