简体   繁体   English

自动装配和对象创建之间有什么区别?

[英]What is the difference between autowiring and object creation?

What is the difference, if I autowire a class and provide value and instantiate an object of class and provide some value? 如果我自动装配一个类并提供值并实例化该类的对象并提供一些值,有什么区别? For example- 例如-

@Autowired
private UserService userService;
userService.findUser(userName, password);

And

User user = new user();
userService.findUser(user.getuserName(),user.getpassword());

What is the difference in Autowiring and sending the data and instantiating the object and sending the data to some service class? 自动装配和发送数据以及实例化对象并将数据发送到某些服务类有什么区别?

I'm trying to clarify the concepts in spring. 我正在尝试在春季阐明概念。

Your example doesn't make a lot of sense; 您的示例没有多大意义; this User class, which looks like some plain data object, isn't adding anything to the second snippet. 这个User类看起来像一些普通的数据对象,没有向第二个片段添加任何内容。

The idea of "autowiring" is that some class, like maybe a Web controller, will need a UserService in order to get its work done. “自动装配”的思想是某些类(例如Web控制器)将需要UserService来完成其工作。 When Spring autowires the UserService , it goes into the context and finds a matching object and provides it to the class that needs it. 当Spring自动装配UserService ,它进入上下文并找到匹配的对象,并将其提供给需要它的类。 This is technically separate from creating the object. 从技术上讲,这与创建对象是分开的。

That said, the best practice is to use constructor injection --simply declare the other objects you need as constructor parameters and annotate the constructor with @Autowired (or @Inject ). 就是说,最佳实践是使用构造函数注入-只需将所需的其他对象声明为构造函数参数,并使用@Autowired (或@Inject )注释该构造函数。 Spring will know to look up all the dependencies you need and call the constructor with them. Spring将知道查找所需的所有依赖项,并使用它们来调用构造函数。 This means that it's also very simple to provide mocks of those objects for testing or development. 这意味着提供用于测试或开发的那些对象的模拟也非常简单。

When you use @Autowired you are leaving it up to the Spring framework to find and instantiate the userService . 使用@Autowired时,将其留给Spring框架来查找和实例化userService This is usually controlled through some configuration file or some other configuration, which allows you to change the behaviour of your application without changing the code itself. 通常,这是通过某些配置文件或其他配置来控制的,这使您可以更改应用程序的行为而无需更改代码本身。

On the other hand, when you instantiate the object yourself, you are specifying which object you are after and what type of class you want. 另一方面,当您自己实例化对象时,您将指定要使用的对象以及所需的类类型。 This could leave you with less ambiguous code since you know what type of object is being initialized, but to make a change in your application's behaviour you would need to change your code. 因为您知道要初始化的对象类型,所以这可能会使代码含糊不清,但是要更改应用程序的行为,就需要更改代码。

In essence, the first option is less coupled than the second option, which is usually the recommended way of building things. 从本质上讲,第一种选择比第二种选择少耦合 ,第二种选择通常是建议的构建方式。

Well, the main difference is that in case u use @Autowired the object is also created, however, it's created by container and container decide when to do that. 好吧,主要区别在于,如果您使用@Autowired,也会创建该对象,但是,它是由容器创建的,容器决定何时执行此操作。 I want to give you a simple example: You have four classes 1,2,3 and 4. Three of them (1,2,3) uses the 4th. 我想举一个简单的例子:您有四个类1,2,3和4。其中三个(1,2,3)使用第4个。 So, if you use new(), it`s hard to decide where to create object(in class 1, or 2, or 3, or even in each of them) of 4th class. 因此,如果您使用new(),则很难决定在哪里创建第4类的对象(在类1、2、3或什至在每个类中)。 Moreover, later you can delete class with object initialization and other 2 classes won't work (in case you created one object). 此外,稍后您可以使用对象初始化删除类,而其他2个类将无法工作(如果您创建了一个对象)。 Autowired annotation injects the object but you don't initialize object in class, so no problems appear This is like the simplest answer. 自动装配注释会注入对象,但您无需在类中初始化对象,因此不会出现任何问题。这就像最简单的答案。

上面的答案很好,我想告诉他们之间的主要区别。自动装配的目的是,如果您要创建对象而又对一个类进行了更改,则避免类之间的依赖关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM