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

com.sinszm.common.SpringContext Maven / Gradle / Ivy

Go to download

基于SpringBoot扩展生态应用公共组件 Copyright © 2020 智慧程序猿 All rights reserved. https://www.sinsz.com

There is a newer version: 1.1.3.RELEASE
Show newest version
package com.sinszm.common;

import org.springframework.context.ApplicationContext;

/**
 * 实例读取器
 *
 * @author chenjianbo
 */
public final class SpringContext {

    private ApplicationContext applicationContext = null;

    private static class Spring {
        private static SpringContext holder = new SpringContext();
    }

    private SpringContext() {
    }

    public static SpringContext instance() {
        return Spring.holder;
    }

    void init(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    private void check() {
        assert this.applicationContext != null;
    }

    public ApplicationContext context() {
        check();
        return this.applicationContext;
    }

    public  T getBean(Class clazz){
        check();
        return context().getBean(clazz);
    }

    public Object getBean(String name){
        check();
        return context().getBean(name);
    }

    public  T getBean(String name,Class clazz){
        check();
        return context().getBean(name, clazz);
    }

    public String activeProfile() {
        return context().getEnvironment().getActiveProfiles()[0];
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy