Understanding dependency injection in java without relying on a framework. What does it look like in pure java code?

StackOverflow https://stackoverflow.com/questions/23599116

Frage

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!

War es hilfreich?

Lösung

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.

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top