spring boot获取ApplicationContext对象

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

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }
}

你可能感兴趣的:(spring,boot,使用示例)