0

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?

Vikas
  • 626
  • 1
  • 10
  • 22
  • Please provide a quote or a link to _All text books say, that the main advantage of dependency injection is that you can modify the application without touching the java code_. – Sotirios Delimanolis Dec 22 '14 at 16:05
  • I don't have a lot of experience here, but everything I've read is that the XML configuration produces XML Hell. The XML configuration idea doesn't really work out as well as some folks hoped. So a lot of architectures just went back to using code, since it's easier. – markspace Dec 22 '14 at 16:07

3 Answers3

1

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.

jalynn2
  • 6,397
  • 2
  • 16
  • 15
  • I understand the need and value of dependency injection. But, I don't see any difference between injection using @AutoWire and a well designed code that instantiates using an interface. – Vikas Dec 23 '14 at 05:18
  • 1
    @vikas - With autowire, Spring is instantiating the dependencies. If you are writing the code that instantiates them, then you must be concerned with satisfying their dependencies, or they must do it themselves. When those dependencies get complex, then you need to code factories, singletons, etc., to ensure that resources are being shared, and so on. Yes, it is possible to do that, but Spring eliminates all that code - all you need is a simple annotation, or some straight-forward XML to handle the complex situations. It also forces you to think that those dependencies are external to you. – jalynn2 Dec 23 '14 at 12:21
1

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.

Community
  • 1
  • 1
wassgren
  • 18,651
  • 6
  • 63
  • 77
  • Thanks Wassgren, for the informative links. I guess the advantage of @Autowired over instantiating, is that it enforces some discipline in the code. – Vikas Dec 23 '14 at 05:24
0

having control over the service instances and loose coupling, you can inject any class that implements the requested interface in the annotation.

aurelius
  • 3,946
  • 7
  • 40
  • 73