4

I'm learning about the Spring framework for Java. Its all about dependency injection. Is there blog or some resource or example I can use to understand RAW Dependency injection? In other words, without annotations or xml or any container. What does Dependency Injection look like in pure java code?

Thank you in advance!

Horse Voice
  • 8,138
  • 15
  • 69
  • 120
  • ... Pass in implementations of interfaces to the classes that use them. Don't complicate things--all it means is that classes are given implementations of things instead of the classes deciding what implementation to use. – Dave Newton May 11 '14 at 23:48
  • [Here][1] is stacoverflow link to understand dependency injection: [1]: http://stackoverflow.com/questions/3334578/what-is-dependency-injection – devOpsTools May 11 '14 at 23:51

2 Answers2

9

It would look like this (assuming all beans are prototype scope):

class BeanToBeInjected {
}

class BeanThatNeedsInjection {
    BeanToBeInjected beanToBeInjected;
    public void setBeanToBeInjected(BeanToBeInjected beanToBeInjected) {
        this.beanToBeInjected = beanToBeInjected;
    }
}

class BeanFactory {
    public Object createBean(String id) {
        if("beanThatNeedsInjection".equals(id) {
            BeanThatNeedsInjection beanThatNeedsInjection = new BeanThatNeedsInjection();
            beanThatNeedsInjection.setBeanToBeInjected(new BeanToBeInjected());
            return beanThatNeedsInjection;
        }
        return null;
    }
}

class MyService {
    public void service() {
        BeanThatNeedsInjection beanThatNeedsInjection =
            new BeanFactory().createBean("beanThatNeedsInjection");
    }
}

Of course, enhanced by reflection and other libraries like cglib to create proxy classes on the fly.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Thank you Luiggi. So clearly demonstrated in the example! What is "enhanced by usage of reflection and other libraries like cglib"? – Horse Voice May 12 '14 at 17:46
  • @TazMan because you will have lot of classes and you won't know for sure which fields to inject. For that purpose, you can make it more generic using reflection. Also, in order to create proxies for the classes to add functionality like transaction management for operations, you need to create the class and its instance *on the fly*, and cglib helps you to achieve that purpose. – Luiggi Mendoza May 12 '14 at 17:53
5

The dependency injection in pure Java is a really simple concept. Suppose you have a classes A and B as follows:

public class A{
    private B instanceOfB = new B();
    public method doSomething(){
        // does something involving B
    }
}

Class A internally instantiates class B and so B becomes A's dependency. If you want to test A in isolation, you cannot do it (because it depends on B). If you want to provide A with a different implementation of B, you have to edit the source code of A, which is not always possible or just not the best idea.

Instead consider injecting a dependency through constructor (it is one of the methods you can choose with Spring). You create a public constructor for A, which takes a B as an argument:

class A{
    private B instanceOfB;
    public A(B instanceOfB){
        this.instanceOfB = instanceOfB;
    }
    public method doSomething(){
        // does something involving B
    }
}

Now, instead of constructing the object inside class, you give it (inject) a parameter in an outside code. The advantage of this is that you can pass a different implementation of B, or a mock for testing:

class B{ // whatever }
class B2 extends B{ // whatever }

A anInstanceOfA = new A(new B()); // this is ok, A will internally use B
A anotherInstanceOfA = new A(new B2()); // and this instance will use B2 instead

If B and B2 implement the same interface or B2 extends B, you can use them interchangeably depending on your needs. This provides a great deal of flexibility. This is basically how dependency injection without a DI container works. Spring might be an overkill for small applications, but the basic idea is really simple, it is a good practice and you should definitely use it.

A good resource to learn basics about Spring (and other things) is Java Brains.

Bartek Maraszek
  • 1,404
  • 2
  • 14
  • 31