2016-03-30 3 views
0

Я использую систему безопасности oAuth2 для весенней весны. И есть по умолчанию декларация OAuth2RestTemplate, которая выглядит какПереопределение по умолчанию OAuth2RestTemplate в контексте Spring

@Bean 
@Primary 
public OAuth2RestTemplate oauth2RestTemplate(OAuth2ClientContext oauth2ClientContext, 
     OAuth2ProtectedResourceDetails details) { 
    OAuth2RestTemplate template = new OAuth2RestTemplate(details, 
      oauth2ClientContext); 
    return template; 
} 

Что мне нужно, чтобы обеспечить обработку пользовательских ошибок, так что я пытаюсь поставить его в контекст как

@Bean 
public OAuth2RestTemplate restTemplate(OAuth2ClientContext oauth2ClientContext,  
     OAuth2ProtectedResourceDetails details) { 
    OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(details, oauth2Context); 

    restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { 
     @Override 
     public void handleError(ClientHttpResponse response) throws IOException { 
      // do some stuff 
     } 
    }); 

    return restTemplate; 
} 

Вопрос здесь в том, что реализация по умолчанию аннотируется как @Primary, и поэтому мне интересно, как это можно переопределить?

ответ

0

Вместо того, чтобы переопределять реализацию по умолчанию, вы, вероятно, можете просто получить экземпляр по умолчанию и задать все необходимые вам свойства. См. Пример ниже.

@Configuration 
public class OverridenConfiguration { 
    @Autowire 
    private OAuth2RestTemplate restTemplate; 

    @PostConstruct 
    public void customSettings() { 
     System.out.println("************** custom settings ***********"); 
     restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { 
      @Override 
      public void handleError(ClientHttpResponse response) throws IOException { 
       // do some stuff 
      } 
     }); 
    } 
} 
Смежные вопросы