Spring Dependency Injection Mechanism.

Spring Dependency Injection Mechanism.

In Object-Oriented Programming, we all have dealt with Object creation at its core.

The purpose of this article is to use Spring Dependency Injection concept to write the clean code.

I will be using a very basic class and while implementing, I might not use best practices from the OOP sphere, so spare me for that.

Case 1 - Dependency Injection Without Spring Boot.

We have the below classes for our use case.

  1. Address Class


public class Address {
    private String street;

    public String getStreet() {
        return street;
    }
    
    public Address(){
        this.street = "123 Main Street";
    }

}        

2. Salary Class


public class Salary {
    private int amount;

    public Salary(){
        this.amount=1000;
    }

    public int getAmount() {
        return amount;
    }
}        

3. User Class

public class User {

    private Address address;
    private Salary salary;

    public User(Address address, Salary salary) {
        this.address = address;
        this.salary = salary;
    }

    public void printDetails(){
        System.out.println("Address  :" +  this.address.getStreet());
        System.out.println("Salary   :" + this.salary.getAmount());
    }


}        

4. SpringBootTest Class

import com.spring.springbasics.dependency_demo.User;
import org.junit.jupiter.api.Test;

import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBasicsApplicationTests {

   @Test
   void contextLoads() {

       Address address = new Address();
       Salary salary = new Salary();
       
       User user = new User(address,salary);
       user.printDetails();
       
       }
       
    }        

Until here, the implementation is pretty simple, as we have 2 classes as Address & Salary, with the getter and we have created a param Constructor with User class, incorporating (Address & Salary objects).

Under contextLoads() we are just creating the objects of those classes and printing the User information by using printDetails().

Case 2 - Dependency Injection With Spring Boot.

  1. Address Class

import org.springframework.stereotype.Component;

@Component
public class Address {
    
    private String street;
    public String getStreet() {
        return street;
    }

    public Address(){
        this.street = "123 Main Street";
    }

}        

2. Salary Class

import org.springframework.stereotype.Component;

@Component
public class Salary {
    private int amount;

    public Salary(){
        this.amount=1000;
    }

    public int getAmount() {
        return amount;
    }
}


        

3. User Class

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class User {

    @Autowired
    Address address;

    @Autowired
    Salary salary;

    public void printDetails(){
        System.out.println("Address  :" +  this.address.getStreet());
        System.out.println("Salary   :" + this.salary.getAmount());
    }

}        

4. SpringBootTest Class

import com.spring.springbasics.dependency_demo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBasicsApplicationTests {

   @Autowired
   User user;

   @Test
   void contextLoads() {
      user.printDetails();
   }
}
        

Now, you see the same class has a clean code, which is easily readable after using Annotations such as @Autowired & @Component from Spring Framework.

Let's understand the concept behind this.

1. @Autowired - So, whenever we inject @Autowired, we instruct Spring to create an Object for that class, so it will create an instance of that particular class.

All the steps which we do in the traditional approach like mentioned in Case 1, are performed by Spring for us, by using @Autowired annotation.

2. @Component is a class-level annotation. We are asking Spring to manage all those object creations, so Spring will scan your project and it will look for all the components that are marked with @Component annotation.

It is used to denote a class as a Component. We can use @Component across the application to mark the beans as Spring’s managed components.

The output of both the case would be like the below:-

No alt text provided for this image

So, in a nutshell, we can say:

Use @Autowired - Whenever you need an instance of an object.

Use @Component - Whenever you want Spring to manage the class.

I hope this post is clear for understanding the basic property of Spring Dependency Injection.

In the future, I will be covering the same implementation with respect to the test automation perspective.

Thanks & Happy Learning :)

Miguel Jimenez

Business Relationship Manager @ Ardan Labs | B.B.A.

10 个月

Shanky... thanks for sharing!

要查看或添加评论,请登录

Shanky Kalra的更多文章

  • What is Idempotency in REST APIs?

    What is Idempotency in REST APIs?

    One of the features in the REST APIs is idempotency, which in general language is described below. Idempotent Operation…

    1 条评论
  • Monitoring of the performance of Jenkins itself with JavaMelody.

    Monitoring of the performance of Jenkins itself with JavaMelody.

    Amidst various jobs spawning in Jenkin's it is crucial to see the gist of all at one place. Jenkins provides us…

  • Unit Testing with xUnit.Net.

    Unit Testing with xUnit.Net.

    Well switching from one language to another seems bit hazy in the beginning, but with the consistency we can achieve a…

    2 条评论
  • Zalenium - A Selenium Grid with Cool Features.

    Zalenium - A Selenium Grid with Cool Features.

    Pre-requisite - Docker Understanding. Well, at some point in time, we have worked upon setting up Node-Hub architecture…

  • How docker Run works internally?

    How docker Run works internally?

    Well in the programming world, we always start with hello world, to embark our journey into that field. Likewise, I…

  • How do OAuth 2.0 Works?

    How do OAuth 2.0 Works?

    Nowadays, API is an inevitable part of Software Architecture and we are creating and testing the applications which are…

    2 条评论
  • Applying Functional Interface in Test Automation

    Applying Functional Interface in Test Automation

    In this article, I will bring into focus on the general use-case of UI Automation with the below Functional Interface:-…

  • Driver Factory implementation with Supplier Functional Interface.

    Driver Factory implementation with Supplier Functional Interface.

    I am sure every one of us has implemented the Browser configurations while creating or maintaining the framework in UI…

  • How I've Really Learned Programming

    How I've Really Learned Programming

    How to learn programming? This is probably the key question everyone asks himself at the beginning of his programming…

    2 条评论

社区洞察

其他会员也浏览了