io.github.lunasaw.sip.common.utils.SpringBeanFactory Maven / Gradle / Ivy
package io.github.lunasaw.sip.common.utils;
import lombok.Getter;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* @author luna
* @description: bean获取工厂,获取spring中的已初始化的bean
*/
@Component
public class SpringBeanFactory implements ApplicationContextAware {
// Spring应用上下文环境
@Getter
private static ApplicationContext applicationContext;
/**
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
SpringBeanFactory.applicationContext = applicationContext;
}
/**
* 获取对象 这里重写了bean方法,起主要作用
*/
public static T getBean(String beanId) throws BeansException {
if (applicationContext == null) {
return null;
}
return (T)applicationContext.getBean(beanId);
}
/**
* 获取当前环境
*/
public static String getActiveProfile() {
return applicationContext.getEnvironment().getActiveProfiles()[0];
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy