声明一个SpringUtil,并且实现ApplicationContextAware接口
在spring config文件中注入这个SpringUtil类即可
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext context)
throws BeansException {
SpringUtil.applicationContext = context;
}
public static Object getBean(String name){
return applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> requiredType) {
return applicationContext.getBean(requiredType);
}
在spring config文件中注入这个SpringUtil类即可