Dependency Injection in Spring


Dependency Injection in Spring

In this blog, we will be learning dependency injection in spring. Dependency injection is one of the core features of Spring that allows loose coupling and enables easier upgrades.


Dependency Injection means providing Dependency to a class via the constructor and setter method.


What is Dependency?

Suppose someone is dependent on someone else to get work done. For example, I am dependent on a mechanic to get my car fixed. Hence, I have a dependency on the mechanic. I can only take my car out if it gets fixed by mechanics.


Likewise, when class A depends on class B to get work done, we can say that class A depends on Class B, or Class B is a dependency of class A.


Why do we inject Dependency?

Whenever we need to use some functionality from different classes, we need to inject the Class a dependency.


If Class A needs to use some functionality of Class B, then we need to provide Class B as a dependency to Class A.


How do we provide Dependency to a class?

We can provide Dependency in three ways:

  • Approach 1: We can extend Class B (if it is not defined as a final).

Providing Dependency by extending a Class

  • Approach 2: We can define Class B as a property in Class A and also initialize it directly.

Providing dependency by initializing a dependency as a property

  • Approach 3: We can inject Class B to Class A via constructor or setter methods.

  • Using Constructor

Providing dependency by injecting through constructor

  • Using Setter

Providing dependency by injecting through setter method


The first two approaches are correct but inefficient because they will result in hard-coupling and will not allow easy upgrades because Dependency is hard-coded.


On the other side, injecting Dependency is a preferred way because it enables loose coupling.


How does Spring Perform Dependency Injection?


Before Spring, we used to implement Dependency Injection on our own. Spring has taken the burden of injecting Dependency from the developer and delegated it to Spring Container.


The IoC Container

The process of taking the burden from developer to spring container of dependency injection is known as Inversion of Control (IoC). If we understand the literal meaning of IoC, then we can say that the control of dependency injection which developer earlier performed is now handled by spring container, i.e., control has been inverse.


There are two interfaces provided by Spring Framework's IoC Container for dependency Injection:

  • ApplicationContext

  • BeanFactory

ApplicationContext is the most used interface as compared with BeanFactory because ApplicationContext is built on the top BeanFactory and it has more enterprise-level features.


Developers may use ApplicationContext when directly dealing with Spring Framework, but in most of the case, we only use @Autowired annotations. The annotation will internally create the context and will inject beans.


@Autowired annotation

@autowired annotation enables automatic dependency injection. This annotation can be used at three places and each will produce the same result i.e., the class will be injected:


Different ways to inject dependency

  • using properties

JDBC template is autowired on AppReposiotry Class

@Repository
public class AppRepository {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    public String greetRepository( String name ) {
        return name;
    }
}

The above repository class is autowired in the Service class.

@Service
public class AppService {

    @Autowired
    private AppRepository repository;

    public String greetService( String name ) {
        return repository.greetRepository(name);
    }
}


  • using setter

@Repository
public class AppRepository {
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setJdbcTemplate(JdbcTemplate template) {
        jdbcTemplate = template;
    }
    
    public String greetRepository( String name ) {
        return name;
    }
}

  • using constructor

@Repository
public class AppRepository {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    AppRepository(JdbcTemplate template) {
        jdbcTemplate = template;
    }

    public String greetRepository( String name ) {
        return name;
    }
}


Key Takeaway

  • Dependency means if someone is dependent on someone or something to get a job done.

  • Dependency Injection is a process of providing dependency to a class.

  • Spring Framework provides ApplicationContext and BeanFactory interface to implement dependency injection. This process is more manual.

  • @autowired annotation automates the process of Dependency Injection.

  • Dependency can be injected via constructor, setter, or properties.

  • As a general rule of thumb, the constructor can be used if we have to inject the only dependency otherwise we can use either setter of the property.

  • Spring uses IoC to implement Dependency Injection.

  • The IoC got its name from its actions, the earlier developer used to provide dependency but now the container automatically injects the dependency i.e; control has been shifted or inverted from developer to Spring Container.


I hope this blog helps you understand dependency injection in general and in spring.


Comments and feedback are most welcomed.


Follow me on Linkedin, Github, Medium Thanks for reading. Happy Learning 😊

199 views

Recent Posts

See All