简体   繁体   English

春天 - 正在使用新的不良做法?

[英]Spring - is using new a bad practice?

Is creating objects by hand , ie using new operator instead of registering Spring bean and using dependency injection considered bad practice? 手动创建对象,即使用new运算符而不是注册Spring bean并使用依赖注入被认为是不好的做法? I mean, does Spring IoC container have to know about all objects in the application? 我的意思是, Spring IoC容器是否必须知道应用程序中的所有对象? If so, why? 如果是这样,为什么?

You want Spring to create beans for classes that : 您希望Spring为以下类创建bean

  • you want/need to inject instance(s) in other beans 你想/需要在其他bean中注入实例
  • you need to inject beans (or dependencies) in their own instances. 您需要在自己的实例中注入bean(或依赖项)。
  • you want them to benefit from Spring features (instantiation management, transaction management, proxy classes Spring empowered such as Repository/Interceptor and so for...) 你希望他们受益于Spring功能(实例化管理,事务管理,代理类Spring赋予权力,如Repository / Interceptor等等......)

Services, controllers or interceptors are example of them. 服务,控制器或拦截器就是它们的一个例子。
For example a controller may need inject a service or an interceptor. 例如,控制器可能需要注入服务或拦截器。
As well as you don't want to handle the instantiation of these classes by implementing yourself the singleton pattern for each one. 同样,您不希望通过实现每个类的单例模式来处理这些类的实例化。 Which could be error-prone and require boiler plate code. 这可能容易出错并需要锅炉板代码。
So you want all of these classes to be beans managed by Spring. 所以你希望所有这些类都是由Spring管理的bean。

But you don't want to Spring create beans for classes that : 但是你不希望Spring为以下类创建bean

  • you don't want/need to inject instance(s) in other beans 你不希望/需要在其他bean中注入实例
  • you don't need to inject beans (or rdependencies) in their own instances 你不需要在自己的实例中注入bean(或rdependencies)
  • you don't need them benefit from Spring features 你不需要他们受益于Spring功能

Entity, DTO, Value Object are example of them. 实体,DTO,价值对象就是它们的例子。

For example an entity never needs to be injected into another entity or in a service as a dependency because entities are not created at the container startup but are generally created inside a method and have a scope limited to the methods lifespan. 例如,实体永远不需要作为依赖项注入到另一个实体或服务中,因为实体不是在容器启动时创建的,而是通常在方法内部创建,并且范围仅限于方法生命周期。
As well as you don't need Spring to create instances which the lifespan is a method. 除了你不需要Spring来创建寿命是一种方法的实例。 The new operator does very well the job. new运营商做得很好。
So defining them as bean instances makes no sense and appears even counter intuitive. 因此将它们定义为bean实例是没有意义的,甚至反直觉。

Object creation should not dependent on classes. 对象创建不应该依赖于类。 When you use new operator it creats a dependency which is pain point for you ie you have to worry about this object creation When u do change in constructor. 当你使用new运算符时,它会创建一个依赖关系,这对你来说是一个痛点,即当你在构造函数中进行更改时,你必须担心这个对象的创建。 Here where the object globalization helps. 对象全球化帮助的地方。 Autowiring the object reduces the dependencies completely. 自动装配对象可以完全减少依赖关系。 If you dont use this feature you will get to know yourself anyways. 如果您不使用此功能,您将会了解自己。

Spring implements the Dependency Injection pattern. Spring实现了依赖注入模式。 You should inject in the container of spring the beans that are going to be used in other classes as dependence to be able to work. 你应该在spring的容器中注入将在其他类中使用的bean作为依赖以便能够工作。 Usually classes that implement interfaces are injected so that if you change the implementation, the classes that use that interface do not know about the change. 通常会注入实现接口的类,以便在更改实现时,使用该接口的类不知道更改。

I recommend you read the post about the dependency injection of Martin Fowler. 我建议你阅读有关Martin Fowler依赖注入的帖子

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

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