新建一个类实现ApplicationContextAware,即可获取ApplicationContext,然后写get方法获取相应的bean,代码如下:
@Componentpublic class SpringContextHolder implements ApplicationContextAware{ private static ApplicationContext context; public static ApplicationContext getContext() { return context; } public static Object getBean(String beanName) { return context.getBean(beanName); } public staticT getBean(Class beanClass) { return context.getBean(beanClass); } @Override public void setApplicationContext(ApplicationContext paramApplicationContext) throws BeansException { context=paramApplicationContext; }}