简体   繁体   中英

Spring - Need for Dependency Injection with Annotations

I am trying to understand basics of Spring dependency injection and auto wiring. 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.

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. With large projects, the XML itself became very large and difficult/cumbersome to maintain. Changes to dependencies in code required corresponding changes in the XML. Auto-wiring was added as a convenience to reduce the size of the XML.

However, auto-wiring does not degrade dependency injection, because XML can be used to override it. 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.

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. 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.

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 .

The D in SOLID stands for Dependency inversion principle which basically means Dependency Injection . 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.

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.

Another way of dealing with dependency injection is also to use Java Configuration via @Configuration and the @Bean annotations (see the Spring docs ). If you are really concerned with adding @Autowired to your code, Java Configuration is less intrusive.

This SO-question gives a good overview of why DI is important and good. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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