Lets create 2 programs of Dependency Injection (DI), first in Core Java and second in Spring and then compare both of them.
package in.sp.beans;
public class Address
{
private String city;
private String state;
// Constructor
public Address(String city, String state)
{
this.city = city;
this.state = state;
}
// Getters
public String getCity()
{
return city;
}
public String getState()
{
return state;
}
@Override
public String toString()
{
return city + ", " + state;
}
}
package in.sp.beans;
public class Student
{
private String name;
private Address address; // Dependency
// Constructor Injection
public Student(String name, Address address)
{
this.name = name;
this.address = address;
}
// Getter methods
public String getName()
{
return name;
}
public Address getAddress()
{
return address;
}
@Override
public String toString()
{
return "Student Name: " + name + ", Address: " + address;
}
}
package in.sp.main;
import in.sp.beans.Address;
import in.sp.beans.Student;
public class MainApp
{
public static void main(String[] args)
{
// Manually creating Address and injecting into Student
Address address = new Address("Panchkula", "Haryana");
Student student = new Student("Deepak", address);
System.out.println(student);
}
}
Student Name: Deepak, Address: Panchkula, Haryana
Now, let's create a Dependency Injection (DI) program in Spring that will eliminate all the disadvantages of the Core Java Dependency Injection program mentioned above.
package in.sp.beans;
import org.springframework.stereotype.Component;
@Component // Marks this class as a Spring-managed bean
public class Address
{
private String city = "Pune";
private String state = "Maharashtra";
// Constructor
public Address() {}
// Getters
public String getCity()
{
return city;
}
public String getState()
{
return state;
}
@Override
public String toString()
{
return city + ", " + state;
}
}
package in.sp.beans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component // Marks this class as a Spring-managed bean
public class Student
{
private String name = "Deepak";
private Address address; // Dependency
// Constructor Injection
@Autowired // Indicates that Spring should inject Address here
public Student(Address address)
{
this.address = address;
}
// Getter methods
public String getName()
{
return name;
}
public Address getAddress()
{
return address;
}
@Override
public String toString()
{
return "Student Name: " + name + ", Address: " + address;
}
}
package in.sp.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "in.sp.beans")
public class AppConfig
{
// No explicit bean definitions needed; @Component classes will be auto-detected
}
package in.sp.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import in.sp.beans.Student;
import in.sp.config.AppConfig;
public class MainApp
{
public static void main(String[] args)
{
// Loading Spring context from annotations
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
// Retrieving the Student bean, which has Address injected
Student student = context.getBean(Student.class);
System.out.println(student);
}
}
Student Name: Deepak, Address: Pune, Maharashtra
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.