简体   繁体   English

春季-需要带注释的依赖注入

[英]Spring - Need for Dependency Injection with Annotations

I am trying to understand basics of Spring dependency injection and auto wiring. 我正在尝试了解Spring依赖项注入和自动接线的基础。 All text books say, that the main advantage of dependency injection is that you can modify the application without touching the java code, by just modifying the XML. 所有教科书都说,依赖注入的主要优点是您可以通过仅修改XML来修改应用程序而无需触及Java代码。

But, when you use annotations, this purpose is defeated! 但是,当您使用批注时,此目的就无法实现! Then what is the big deal? 那有什么大不了的? Why not just instantiate it than having additional code for injection? 为什么不仅仅实例化它,而没有其他注入代码?

In earlier versions of Spring, all injection had to be done using XML. 在Spring的早期版本中,所有注入都必须使用XML完成。 With large projects, the XML itself became very large and difficult/cumbersome to maintain. 在大型项目中,XML本身变得非常大,并且难以维护。 Changes to dependencies in code required corresponding changes in the XML. 要更改代码中的依存关系,就需要对XML进行相应的更改。 Auto-wiring was added as a convenience to reduce the size of the XML. 添加了自动装配以方便减小XML的大小。

However, auto-wiring does not degrade dependency injection, because XML can be used to override it. 但是,自动装配不会降低依赖注入,因为可以使用XML来覆盖它。 This gives the original flexibility of the XML configuration with the convenience of allowing Spring to default the common case where there is only one possible bean in the context that implements the interface. 这提供了XML配置的原始灵活性,并具有方便Spring缺省的默认情况(在该情况下,实现接口的上下文中只有一个bean)的便利。

Your question seems to be asking why we want dependency injection at all: "Why not just instantiate it than having additional code for injection?". 您的问题似乎是在问为什么我们要进行依赖项注入:“为什么不仅仅实例化它,而要有附加的注入代码?”。 One of the most common uses of dependency injection is in unit testing: A test program injects a test version of a dependency into the object under test to break further dependencies. 依赖项注入最常见的用途之一是在单元测试中:测试程序将依赖项的测试版本注入到被测对象中,以打破其他依赖项。 For example, if service class A makes a call to service class B, and B relies on DB objects, other services, file systems, etc., then testing A becomes very difficult if A instantiates B directly because you need to make sure all of B's dependencies are met. 例如,如果服务类A调用服务类B,并且B依赖于数据库对象,其他服务,文件系统等,则如果A直接实例化B,则测试A变得非常困难,因为您需要确保所有B的依赖关系得到满足。 If A is coded to an interface iB instead of the Class B, then the unit test code can inject a instance of a test class that implements iB and responds to method calls without any further dependencies. 如果将A编码为接口iB而不是类B,则单元测试代码可以注入实现iB并响应方法调用的测试类的实例,而无需任何其他依赖。

I believe that there are many developers that believes that if you want to build long lasting maintainable code you should abide by the SOLID-principles . 我相信有很多开发人员认为,如果您要构建持久的可维护代码,则应遵守SOLID原理

The D in SOLID stands for Dependency inversion principle which basically means Dependency Injection . SOLID中的D表示依赖关系反转原理 ,它基本上意味着依赖关系注入 So, Dependency Injection is a good thing if you want to keep your code clean and separate object creation from the actual business code. 因此,如果要保持代码整洁并将对象创建与实际业务代码分开,则依赖注入是一件好事。 Furthermore, DI makes your code more testable according to me. 此外,根据我的说法,DI使您的代码更可测试。

You are right that annotations such as @Autowired is intrusive on your code but you do not have to change the logic or anything , simply annotate the methods. 没错,诸如@Autowired注释对您的代码而言是侵入性的, 但您不必更改逻辑或任何内容 ,只需注释方法即可。

Another way of dealing with dependency injection is also to use Java Configuration via @Configuration and the @Bean annotations (see the Spring docs ). 处理依赖项注入的另一种方法也是通过@Configuration@Bean批注使用Java Configuration(请参阅Spring docs )。 If you are really concerned with adding @Autowired to your code, Java Configuration is less intrusive. 如果您真的很想在代码中添加@Autowired ,那么Java配置的介入性就会降低。

This SO-question gives a good overview of why DI is important and good. 这样的SO问题很好地概述了DI为什么重要和良好的原因。 One quote from that post is: 该帖子的一句话是:

Dependency injection is basically providing the objects that an object needs (its dependencies) instead of having it construct them itself. 依赖注入基本上是提供对象需要的对象(其依赖),而不是让对象自己构造它们。 It's a very useful technique for testing, since it allows dependencies to be mocked or stubbed out. 这是一种非常有用的测试技术,因为它允许对依赖项进行模拟或存根。

通过控制服务实例和松散耦合,您可以在注释中注入任何实现请求的接口的类。

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

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