有些人实现了这个接口,但发现ApplicationContext为null,这是因为实现了该接口的这个类本身也需要在Spring容器中时行配置,否则Spring是不会将ApplicationContext实例放到其中的。
@Component("springApplicationContext") public class ApplicationContextHelper implements ApplicationContextAware { private static ApplicationContext appCtx; @Override public void setApplicationContext(ApplicationContext applicationContext ) throws BeansException { appCtx = applicationContext ; } public static Object getBean(String beanName) { return appCtx.getBean(beanName); } }