@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public SpringContextUtil() {
}
public static <T> T getBean(Class<T> clzName) throws BeansException {
return applicationContext.getBean(clzName);
}
public void setApplicationContext(ApplicationContext applicationContext) {
if (applicationContext == null) {
applicationContext = applicationContext;
}
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
public static Object getBean(String name, Class requiredType) {
return applicationContext.getBean(name, requiredType);
}
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
public static boolean isSingleton(String name) {
return applicationContext.isSingleton(name);
}
public static Class getType(String name) {
return applicationContext.getType(name);
}
public static String[] getAliases(String name) {
return applicationContext.getAliases(name);
}
}
spring boot ApplicationContextAware獲取應用上下文
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.