java Spring @Autowired 与使用“new”关键字创建对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30908648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring @Autowired vs using 'new' keyword to create Object
提问by Tushar
I am learning Spring and building some experiment application. I am confused on where to use @Autowired for creating the Object.
我正在学习 Spring 并构建一些实验应用程序。我对在哪里使用 @Autowired 创建对象感到困惑。
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.
我明白它促进了松散耦合,并且每次都会创建一个新对象,而不是“new”关键字所做的。
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
但是对于我们需要在我们的应用程序中使用的第三方对象,我们应该怎么做。例如,我正在使用一个休息 API,因为我需要初始化三个类,就像这样
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.
这段代码正在为 RestTemplate、HttpHeaders 和 HttpEntity 创建新的对象。使用此代码,每次我调用其余 API 时,它都会创建三个新对象。这是正确的方法还是我应该将它们标记为@Autowired。请详细说明。
回答by m4ktub
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.
的典型用途@Autowire
是在初始化 bean 时使用单例依赖项自动填充属性。无论是您的代码还是第三方类都无关紧要。您需要考虑它是否是程序逻辑的一部分,或者它是否真的是一个应该初始化一次并重用的依赖项。
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.
如果您RestTemplate
需要在每个之前进行相同的初始化,exchange
那么您可以考虑@Autowire
在 Spring 的配置中使用和初始化 bean。在这个意义上,它类似于 a DataSource
,例如。但是,如果您将其视为用作程序逻辑一部分的实用程序类(如连接或文件),则不要将其视为@Autowire
. 它会使您的程序更加复杂,而没有显着的收益。
回答by barun
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.
您需要使用“范围”。默认情况下,当您使用 @Autowired spring bean 时,范围是单例。这意味着 spring 会在您使用 @Autowired 的地方注入相同的单例对象。通过制作范围原型,您可以指示 Spring 为每个 @Autowired 注入创建新对象。
please refer to spring-autowired-instantiate-new-bean
回答by Paolof76
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.
由于 restTemplate 是您将要重用的唯一类,因此,将其放入范围为 Singleton(默认范围,因此只是普通 bean)的 bean 中可能是明智之举。每次调用使用此客户端的方法时,都可以创建另外两个类。请注意,您也可以将 Entity 和 Header 作为参数传递......所以我不明白您为什么要使用 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);
}
}