What's the proper way of creating RestTemplate
instance in Spring
?
Let's say I have a service where I do multi-thread requests via rest template.
I found various variants in different tutorials:
1) create it when it is needed:
RestTemplate restTemplate = new RestTemplate();
2) declare it as bean:
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
(or what is the same way - in XML configuration file).
and then autowire it... Will it be always thread-safe in this case?
Is any performance difference? What is more "spring" style?
RestTemplate
. That's a choice you need to make. Performance is implied by thread safety. If the object is thread safe, then it makes no sense to keep initializing new instances. It only makes sense if you need custom configurations for each, but we can't answer that because it's not part of your question. – Sotirios Delimanolis Feb 8 '17 at 18:25