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.
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.
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:-
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 :)
Business Relationship Manager @ Ardan Labs | B.B.A.
10 个月Shanky... thanks for sharing!