title: Ribbon LB / OAuth2RestTemplate
body: <p>I'm trying to invoke the load balancer to get access to a service instance in order to build the OAuth2RestTemplate access token uri within my configuration class, but for some reason I keep getting exceptions when the bean is instantiated. Has anyone come across this issue before? Or perhaps may have some insight?</p>
  
  <p>Factory method 'restTemplate' threw exception; nested exception is java.lang.IllegalStateException: Unable to locate ILoadBalancer for service: service</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource [com/apop/services/config/rest/RestConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.oauth2.client.OAuth2RestTemplate]: Factory method 'serviceRestTemplate' threw exception; nested exception is java.lang.IllegalStateException: Unable to locate ILoadBalancer for service: service
  </code></pre>
  
  <p>Also here are my service and configuration classes:</p>
  
  <pre><code>@Service
  public class ServiceIntegration implements Service, Serializable {
  
  
  @Resource(name = "serviceRestTemplate")
  OAuth2RestTemplate serviceRestTemplate;
  
  @Autowired
  private LoadBalancerClient balancerClient;
  
  @Value("${service}")
  private String service;
  
  private ResponseExceptionHelper responseExceptionHelper = new ResponseExceptionHelper();
  
  public ResponseExceptionHelper getResponseExceptionHelper() {
      return responseExceptionHelper;
  }
  
  public void setResponseExceptionHelper(
          ResponseExceptionHelper responseExceptionHelper) {
      this.responseExceptionHelper = responseExceptionHelper;
  }
  
  /**
   * @param payload
   * @param path
   * @param method
   * @return
   * @throws ResponseException
   */
  private String execute(OAuth2RestTemplate restTemplate, String endPoint, String payload, HttpMethod method)
          throws ResponseException {
      HttpHeaders headers = new HttpHeaders();
  
      headers.setContentType(MediaType.APPLICATION_JSON);
      HttpEntity&lt;String&gt; requestEntity = new HttpEntity&lt;String&gt;(payload, headers);
  
      String response = null;
      try {
  
          ServiceInstance serviceInstance = balancerClient.choose(service);
          String uri = String.format("http://%s:%s/%s", serviceInstance.getHost(), serviceInstance.getPort(), service) + endPoint;
          ResponseEntity&lt;String&gt; responseEntity = restTemplate.exchange(uri, method, requestEntity, String.class);
          if (responseEntity != null) {
              response = responseEntity.getBody();
          }
      } catch (Exception e) {
          logger.error(error, e);
          throw e;
      }
  
      logger.info(response);
      return response;
  }
  
  }
  </code></pre>
  
  <p>Config:</p>
  
  <pre><code>@Configuration
  @ComponentScan
  @EnableAutoConfiguration
  public class RestConfiguration {
  
  
  @Value("${service}")
  private String service;
  
  
  @Value("${token.service.endpoint}")
  private String tokenServiceEndpoint;
  
  
  @Value("${clientId}")
  private String clientId;
  
  @Value("${clientSecret}")
  private String clientSecret;
  
  @Value("${grant}")
  private String grant;
  
  @Autowired
  private LoadBalancerClient balancerClient;
  
  
  @Bean
  public OAuth2RestTemplate serviceRestTemplate() {
      ClientCredentialsResourceDetails resources = getClientDetails();
      OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resources, new DefaultOAuth2ClientContext());
      return restTemplate;
  }
  
  
  public ClientCredentialsResourceDetails getClientDetails() {
      ServiceInstance serviceInstance = balancerClient.choose(service);
      String uri = String.format("http://%s:%s/%s", serviceInstance.getHost(), serviceInstance.getPort(), legacyService) + tokenServiceEndpoint;
  
      ClientCredentialsResourceDetails resource = new ClientCredentialsResourceDetails();
      resource.setAccessTokenUri(uri);
      resource.setClientSecret(clientId);
      resource.setClientId(clientSecret);
      resource.setGrantType(grant);
      return resource;
  }
  
  }
  </code></pre>