0

xml設定を使用してBeanを定義していません。コンポーネントのスキャンと自動配線を使用して、依存関係を定義して注入します。 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証明書を使用する必要がある場合は、もう少しコードが必要です(この場合は教えてください)