title: How to retrieve the Application Context in Spring Boot 2
body: <p>I have this <code>ApplicationContextProvider</code> class defined along with the <code>MyApplication.java</code> (entry point where the application is run):</p>
  
  <pre><code>package com.company.my.app;
  
  import org.springframework.beans.BeansException;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.ApplicationContextAware;
  import org.springframework.stereotype.Component;
  
  
  @Component
  public class ApplicationContextProvider implements ApplicationContextAware {
  
    private ApplicationContext applicationContext;
  
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
      this.applicationContext = applicationContext;
    }
  
    public ApplicationContext getContext() {
      return applicationContext;
    }
  }
  </code></pre>
  
  <p>Have the package <code>restapi</code> with two classes in it (<code>Greeting</code> is just a class to hold data):</p>
  
  <pre><code>package com.company.my.app.restapi;
  
  import com.company.my.app.ApplicationContextProvider;
  import io.micrometer.core.instrument.Counter;
  import java.util.concurrent.atomic.AtomicLong;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.context.ApplicationContext;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.bind.annotation.RestController;
  
  
  @RestController
  public class GreetingController {
  
    private static final Logger LOG = LoggerFactory.getLogger(GreetingController.class);
  
    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();
  
    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
  
      ApplicationContextProvider acp = new ApplicationContextProvider();
      ApplicationContext context = acp.getContext();
  
      if (context == null) LOG.info("app context is NULL");
  
      Counter bean = context.getBean(Counter.class);
      bean.increment();
  
      return new Greeting(counter.incrementAndGet(),
          String.format(template, name));
    }
  }
  </code></pre>
  
  <p>Finally the <code>MyApplication</code> class is:</p>
  
  <pre><code>package com.company.my.app;
  
  import io.micrometer.core.instrument.Metrics;
  import io.micrometer.core.instrument.binder.MeterBinder;
  import java.util.Arrays;
  import java.util.List;
  import java.util.stream.Collectors;
  import io.micrometer.core.instrument.MeterRegistry;
  import io.micrometer.core.instrument.Counter;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  
  @SpringBootApplication
  public class MyApplication {
  
    @Bean
    public MeterBinder exampleMeterBinder() {
      return (meterRegistry) -&gt; Counter.builder("my.counter")
          .description("my simple counter")
          .register(meterRegistry);
    }
  
    @Configuration
    public class CounterConfig {
      @Bean
      public Counter simpleCounter(MeterRegistry registry) {
        return registry.counter("my.counter");
      }
    }
  
    public static void main(String[] args) {
      SpringApplication.run(MyApplication.class, args);
    }    
  }
  </code></pre>
  
  <p>When I run the app and call <code>http://localhost:8081/greeting</code> in my browser, it crashes printing <code>app context is NULL</code>. How do I get the application context? I need it to retrieve the simple counter bean.</p>