在 Spring 应用中,可以使用以下方式获取 ApplicationContext
对象:
通过实现
ApplicationContextAware
接口,并在setApplicationContext
方法中获取ApplicationContext
对象。在类中注入
ApplicationContext
对象,可以使用@Autowired
注解。通过调用
SpringContextHolder.getApplicationContext()
方法获取。在类中使用静态方法
WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc)
获取。通过调用
ContextLoader.getCurrentWebApplicationContext()
方法获取。
希望这些方法能帮到你。