package com.inspect.upstream.util;
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
import org.springframework.context.ApplicationContextAware;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
@Service
|
|
public class SpringApplicationContext implements ApplicationContextAware {
|
|
|
|
private static ApplicationContext applicationContext;
|
|
|
|
@Override
|
|
public void setApplicationContext(ApplicationContext applicationContext) {
|
|
SpringApplicationContext.applicationContext = applicationContext;
|
|
}
|
|
|
|
public static ApplicationContext getApplicationContext() {
|
|
checkApplicationContext();
|
|
return applicationContext;
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
public static <T> T getBean(String name) {
|
|
checkApplicationContext();
|
|
return (T) applicationContext.getBean(name);
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
public static <T> T getBean(Class<T> clazz) {
|
|
checkApplicationContext();
|
|
return (T) applicationContext.getBeansOfType(clazz);
|
|
}
|
|
|
|
|
|
public static void cleanApplicationContext() {
|
|
applicationContext = null;
|
|
}
|
|
|
|
private static void checkApplicationContext() {
|
|
if (applicationContext == null) {
|
|
throw new IllegalStateException(
|
|
"applicationContext未注入,请在spring的context.xml中定义SpringContextHolder");
|
|
}
|
|
}
|
|
|
|
}
|