加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的
public void setApplicationContext(ApplicationContext context) throws BeansException
方法,获得ApplicationContext 对象。
前提必须在Spring配置文件中指定该类。
调用:
ICartManager cartManager = SpringContextHolder.getBean("cartManager");
本文介绍了一种在Spring框架中通过实现ApplicationContextAware接口并利用SpringContextHolder类来获取ApplicationContext上下文的方法。这种方式允许开发者在应用程序的任何地方都能方便地访问到Spring的Bean实例。
5083

被折叠的 条评论
为什么被折叠?



