Dependency Injection in Spring Framework

Dependency Injection in Spring Framework

Dependency Injection is a very important thing in Java EE, especially when we use Spring Framework. In short, control and dependency are kept in a XML configuration file, not in a Java file as in a normal Java class is. The flow of control is inverted because it’s in an external system that manages all dependencies. So let’s see all the DI ways that can be used in our projects.

0/ with using a normal Java style

package com.linaittech;

public class Mirek {

private Murek mureczek;

public Mirek(){

mureczek = new Murek();

}

}

All who work with Java language know this style. Desktop apps use it. But Java EE require somewhat different solution.

1/ with using an @Inject annotation in the property to be injected

package com.linaittech;

import javax.inject.Inject;

public class Mirek {

@Inject

private Murek mureczek;

 }

We import the Inject interface, then in the Mirek class, we use Dependency Injection. The @Inject annotation tell us that the field mureczek comes from the Murek class. In other words, it’s injected from Murek to Mirek.

2/ with using the setter method

package com.linaittech;

import javax.inject.Inject;

public class Mirek {

private Murek mureczek

@Inject

public void setMureczek(Murek mureczek{

this.mureczek = mureczek

}

}

So we can use the setter method, too. Again, we import the Inject interface; we have the @inject annotation; but we send the field mruczek from the class Murek to the setMureczek setter method. It’s clear.

3/ with using the constructor method

package com.linaittech;

import javax.inject.Inject;

public class Mirek {

private final Murek mureczek

@Inject

public Mirek(Murek mureczek)

this.mureczek = mureczek

}

}

The same mechanism, but this time we use the constructor. However, we can only have one such constructor (with Dependency Injection).

4/ with using @Any qualifier

package com.linaittech;

import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;

public class Mirek {

@Inject

public void getAllBeanImplement(@Any Instance<Murek> beans) {

for (Murek mureczek : beans) {

System.out.println(mureczek.getClass().getCanonicalName());

}

}

}

We have two annotations: @Inject and @Any. We can use the @Any qualifier when we have multiple implementations of one interface and we want to inject them all. Using this annotation the container will inject all implementations of the specified interface. It’s an important difference. We use for loop to go through all the implementations of our interface. Then, we can use such an object to get to its methods or variables.

5/ with using proxies

The proxy handles all calls to the injected bean. It’s a very transparent method. The proxy is an extended bean class with all non private methods overridden. Primitive types cannot be injected. Your bean class must have a non-private default constructor and must not be final nor have final methods.

Sometimes, we can mix DI types if need be. However, remember that you should use constructors with DI for mandatory dependencies and setters with DI for optional dependencies. It’s a good way of programming.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s