title: Spring Boot dependency injecting with request scoped beans
body: <p>I am trying to dependency inject one bean into another, but I would like to do it by simply Autowiring directly into the class, instead of via the "@Configuration class".  Here is the controller class:</p>
  
  <pre><code>@Controller
  public class RestfulSourceController {
  
      @Autowired
      Response response;
  
      @RequestMapping(value="/rest", method=RequestMethod.GET, produces="application/json")
      @ResponseBody
      public Object greeting() {
          return response.getResponse();
      }
  
  }
  </code></pre>
  
  <p>And here are to "@Configuration" classes with a bean declared inside each</p>
  
  <pre><code>@Configuration
  class RequestConfigurationBeans {
  
      @Autowired
      private ServicesRepository servicesRepo;
  
      @Autowired
      private HttpServletRequest request;
  
      @Bean(name = 'requestServiceConfig')
      @Scope(value=WebApplicationContext.SCOPE_REQUEST, proxyMode=ScopedProxyMode.TARGET_CLASS)
      public ServiceModel requestServiceConfig(){
          String serviceName = RequestUtil.getServiceName(this.request)
          ServiceModel serviceModel = servicesRepo.findByName(serviceName)
          return serviceModel
      }
  
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>@Configuration
  public class ServletFilterBeans {
  
      /* I don't want to autowire here, instead I want to autowire in the Response class directly, instead of passing the bean reference into the constructor
      @Autowired
      ServiceModel requestServiceConfig
      */
  
      @Bean
      @Scope(value=WebApplicationContext.SCOPE_REQUEST, proxyMode=ScopedProxyMode.TARGET_CLASS)
      public Response response(){
          return new Response(/*requestServiceConfig*/);
      }
  
  }
  </code></pre>
  
  <p>And finally here is the Response class:</p>
  
  <pre><code>class Response {
  
      //I want to Autowire the bean instead of passing the reference to the constructor
      @Autowired
      ServiceModel requestServiceConfig
  
      Object response
  
      public Response(/*ServiceModel requestServiceConfig*/){
          //this.requestServiceConfig = requestServiceConfig
  
          if (requestServiceConfig.getType() == 'rest'){
              this.response = getRestfulSource()
          }
  
      }
  
  
      private Object getRestfulSource(){
          RestTemplate restTemplate = new RestTemplate()
          String url = requestServiceConfig.sourceInfo.get('url')
          return restTemplate.getForObject(url, Object.class)
      }
  }
  </code></pre>
  
  <p>However, when I set up the dependency injection like this, I get a null pointer exception because the autowired bean "requestServiceConfig" is not instantiated. How can I dependency inject the bean with autowiring so that I don't have to pass the reference to the bean via the constructor?</p>