package com.lf.server.helper;
|
|
import org.springframework.beans.BeansException;
|
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.stereotype.Component;
|
|
/**
|
* Spring上下文帮助类
|
* @author WWW
|
*/
|
//@Component
|
@SuppressWarnings("AlibabaCommentsMustBeJavadocFormat")
|
public class SpringContextHelper implements ApplicationContextAware {
|
private static ApplicationContext context = null;
|
|
/**
|
* 设置应用程序上下文
|
*
|
* @param applicationContext
|
* @throws BeansException
|
*/
|
@Override
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
context = applicationContext;
|
}
|
|
/**
|
* 根据名称获取Bean
|
*
|
* @param name 类名称
|
* @return
|
*/
|
public static <T> T getBean(String name) {
|
return (T) context.getBean(name);
|
}
|
|
/**
|
* 根据类型获取Bean
|
*
|
* @param clazz 类
|
* @param <T> 泛型
|
* @return
|
*/
|
public static <T> T getBean(Class<T> clazz) {
|
return context.getBean(clazz);
|
}
|
|
/**
|
* 获取当前环境
|
*
|
* @return Profile
|
*/
|
public static String getActiveProfile() {
|
return context.getEnvironment().getActiveProfiles()[0];
|
}
|
}
|