- @Autowired注入
@Autowired
private ApplicationContext applicationContext;
- 实现ApplicationContextAware接口
@Component
public class ApplicationContextHolder implements ApplicationContextAware {
private ApplicationContext applicationContext;
//容器启动后自动调用该方法
@Override
public void setApplicationContext(ApplicationContext applicationContext){
this.applicationContext = applicationContext;
}
public ApplicationContext getApplicationContext() {
return applicationContext;
}
}