0

bean을 정의하기 위해 xml 구성을 사용하지 않습니다. 대신 구성 요소 스캐닝과 autowire를 사용하여 종속성을 정의하고 주입합니다. RestTemplate은 springframework의 일부입니다. 이 수업을 어떻게 삽입 할 수 있습니까?RestTemplate 주입 방법

+0

구성 클래스에'@ Bean' 주석 메소드를 정의하십시오. RestTemplate. http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-java –

+0

이 질문에 대한 코드를 정확히 확인하십시오. http : // stackoverflow.com/questions/40473146/java-spring-interceptor-with-no-xml –

답변

3

@Configuration 클래스에있는 다른 @Bean과 흡사하고 @Autowire를 삽입하십시오.하지만 Spring 설명서를 좀 더 읽어야한다고 생각합니다.

@Bean 
    public RestTemplate restTemplate() { 
     RestTemplate template = new RestTemplate(); 
     PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); 
     connectionManager.setMaxTotal(100); 
     connectionManager.setDefaultMaxPerRoute(6); 
     template.setRequestFactory(new HttpComponentsClientHttpRequestFactory(HttpClients.custom().setConnectionManager(connectionManager).build())); 
     return template; 
    } 

거의 항상 연결 풀링을 사용하기 위해 Apache HttpClient와 함께 사용하려고합니다. 자체 서명 된 https 인증서와 함께 사용해야하는 경우 좀 더 많은 코드가 필요합니다. (이 경우 알려주십시오.)