Spring @Autowired vs using 'new' keyword t

2020-06-24 08:28发布

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.

3条回答
手持菜刀,她持情操
2楼-- · 2020-06-24 09:23

You need to use "scope".By default when you use @Autowired spring bean scope is singleton. That means spring injects the same singleton object where ever you use @Autowired. By making scope prototype you are instructing Spring to create new objects for each @Autowired injection.

please refer to spring-autowired-instantiate-new-bean

查看更多
倾城 Initia
3楼-- · 2020-06-24 09:24

The typical use of @Autowire is to automatically fill a property, when initializing a bean, with a singleton dependency. It does not matter if it's your code or a Third Party class. You need to consider if it is part of your program's logic or if it is really a dependency that should be initialized once and reused.

If your RestTemplate needs to have the same initialization before each exchange then you can consider using @Autowire and initialize the bean in Spring's configuration. In this sense it would be similar to a DataSource, for example. But if you see it as an utility class that is used as part of the program's logic (like a connection or a file) then do not consider it for @Autowire. It will make your program more complex without a significant gain.

查看更多
Emotional °昔
4楼-- · 2020-06-24 09:26

As the restTemplate is the only class that you are going to reuse, probably, it would be savvy to put it in a bean with scope Singleton (default scope so just a normal bean). The other two classes can be created each time you call the method that uses this client. Note that you could also pass the Entity and Header as parameters... So I don't see why you want to use spring.

class MyClass {
    @Autowired
    private RestTemplate restTemplate;

    public void callTheRestClient() {
         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); 
    }

}
查看更多
登录 后发表回答