欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【SpringBoot动态加载Spring容器的类】 博客分类: 编程语言--JAVA之SpringSpring-Boot  

程序员文章站 2024-03-20 19:34:16
...
package com.xxx

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

import java.io.Serializable;

/**
 * SpringBoot动态加载Spring容器的类
* 类似JalorApplicationContext
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware, Serializable {

    /**
     * 上下文
*/
private static ApplicationContext context;

    @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    public static <T> T getBean(Class<T> beanClass) {
        return context.getBean(beanClass);
    }
}