Evaluation Article
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.spring.di;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class DepartmentService {
|
||||
public String getDepartment() {
|
||||
return "Management";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.spring.di;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component("employeeControllerWithConstructorBasedDI")
|
||||
public class EmployeeControllerWithConstructorBasedDI {
|
||||
|
||||
private DepartmentService departmentService;
|
||||
|
||||
@Autowired
|
||||
public EmployeeControllerWithConstructorBasedDI(DepartmentService departmentService) {
|
||||
this.departmentService = departmentService;
|
||||
}
|
||||
|
||||
public String getEmployeeDetails() {
|
||||
return "Employee: A, Department: " + departmentService.getDepartment();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.spring.di;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component("employeeControllerWithSetterBasedDI")
|
||||
public class EmployeeControllerWithSetterBasedDI {
|
||||
private DepartmentService departmentService;
|
||||
|
||||
@Autowired
|
||||
public void setDepartmentService(DepartmentService departmentService) {
|
||||
this.departmentService = departmentService;
|
||||
}
|
||||
|
||||
public String getEmployeeDetails() {
|
||||
return "Employee: A, Department: " + departmentService.getDepartment();
|
||||
}
|
||||
}
|
||||
7
spring-core/src/main/resources/spring-di.xml
Normal file
7
spring-core/src/main/resources/spring-di.xml
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
<context:component-scan base-package="com.baeldung.spring.di"/>
|
||||
</beans>
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.spring.di;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class DepdencyInjectionUnitTest {
|
||||
|
||||
private ApplicationContext context;
|
||||
|
||||
@Before
|
||||
public void oneTimeSetup() {
|
||||
context = new ClassPathXmlApplicationContext("spring-di.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConstructorBasedDI_ThenDependencyValid() {
|
||||
EmployeeControllerWithConstructorBasedDI employee = (EmployeeControllerWithConstructorBasedDI) context
|
||||
.getBean("employeeControllerWithConstructorBasedDI");
|
||||
assertEquals("Employee: A, Department: Management", employee.getEmployeeDetails());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSetterBasedDI_ThenDependencyValid() {
|
||||
EmployeeControllerWithSetterBasedDI employee = (EmployeeControllerWithSetterBasedDI) context
|
||||
.getBean("employeeControllerWithSetterBasedDI");
|
||||
assertEquals("Employee: A, Department: Management", employee.getEmployeeDetails());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user