I am learning Spring and building some experiment application. I am confused on where to use @Autowired for creating the Object.
I get the part that it promotes loose coupling and also does create a new Object Every time as opposed to what 'new' keyword do.
But what should we do for the Third Party Objects which we need to use in our Application. For example I am consuming a rest API for that I need to Initialise three Classes , something like this
RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); HttpEntity<String> entity = new HttpEntity<String>("parameters", headers); restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
This chunk of code is creating new Objects for RestTemplate, HttpHeaders and HttpEntity. With this code it will create three new objects everytime I call the rest API. Is it the correct approach or should I mark them @Autowired. Please elaborate.