Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.baeldung.app;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.baeldung.configuration.DependencyInjectionSpring;
import com.baeldung.domain.Course;
import com.baeldung.domain.Department;
import com.baeldung.domain.Employee;
import com.baeldung.domain.Student;

public class DependencyInjectionApp {

public static void main(String[] args) {

ApplicationContext context = new AnnotationConfigApplicationContext(DependencyInjectionSpring.class);

System.out.println("Mandatory Dependency");
Employee employee = context.getBean(Employee.class);
employee.setId(1);
employee.setName("Robert Langdon");
Department department = context.getBean(Department.class);
System.out.println("Department Name: "+department.getName());
System.out.println("Employee in department: "+department.getEmployee().getName());

System.out.println("Optional Dependency");
Student student = context.getBean(Student.class);
student.setId(1);
student.setName("Albert Einstein");
Course course = context.getBean(Course.class);
System.out.println("Course:"+course.getName());
System.out.println("Student enrolled: "+course.getStudent().getName());
((ConfigurableApplicationContext)context).close();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.configuration;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.baeldung.domain")
public class DependencyInjectionSpring {}
42 changes: 42 additions & 0 deletions spring-core/src/main/java/com/baeldung/domain/Course.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.baeldung.domain;

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

@Component
public class Course {
private int id;
private String name = "Physics";

/* Dependency/collaborator */
private Student student;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Student getStudent() {
return student;
}

/*
* Optional dependency since a course may or
* may not have student enrolled into it
*/
@Autowired(required = false)
public void setStudent(Student student) {
this.student = student;
}
}
34 changes: 34 additions & 0 deletions spring-core/src/main/java/com/baeldung/domain/Department.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.baeldung.domain;

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

@Component
public class Department {

private String name = "Technology";

/* Dependency/collaborator */
private Employee employee;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Employee getEmployee() {
return employee;
}

/* Mandatory dependency since a
* department will have an employee.
*/
@Autowired
public Department(Employee employee) {
this.employee = employee;
}

}
27 changes: 27 additions & 0 deletions spring-core/src/main/java/com/baeldung/domain/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.domain;

import org.springframework.stereotype.Component;

@Component
public class Employee {

private int id = 1;
private String name = "Robert Langdon";

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
25 changes: 25 additions & 0 deletions spring-core/src/main/java/com/baeldung/domain/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.baeldung.domain;

import org.springframework.stereotype.Component;

@Component
public class Student {
private int id = 1;
private String name = "Albert Einstein";

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.baeldung.dependencyinjectiontypes;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.baeldung.configuration.DependencyInjectionSpring;
import com.baeldung.domain.Department;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DependencyInjectionSpring.class)
public class WhenUsingConstructorInjection {

@Autowired
private Department department;

@Test
public void shouldReturnDepartmentName() {
Assert.assertEquals("Technology", department.getName());
}

@Test
public void shouldReturnEmployeeName() {
Assert.assertEquals("Robert Langdon", department.getEmployee()
.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.baeldung.dependencyinjectiontypes;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.baeldung.configuration.DependencyInjectionSpring;
import com.baeldung.domain.Course;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DependencyInjectionSpring.class)
public class WhenUsingSetterInjection {

@Autowired
private Course course;

@Test
public void shouldReturnCourseName() {
Assert.assertEquals("Physics", course.getName());
}

@Test
public void shouldReturnStudentName() {
Assert.assertEquals("Albert Einstein", course.getStudent()
.getName());
}
}