Rav*_*avi 7 spring dependency-injection autowired resttemplate spring-bean
我没有使用xml配置来定义bean.而是使用组件扫描和autowire来定义和注入依赖项.RestTemplate是springframework的一部分.我怎么能注入这个课程?
你像@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;
}
Run Code Online (Sandbox Code Playgroud)
您几乎总是希望将它与Apache HttpClient一起使用,以便获得连接池.如果您需要将其与自签名https证书一起使用,则需要更多代码(如果是这种情况,请告诉我)
| 归档时间: |
|
| 查看次数: |
5612 次 |
| 最近记录: |