Skip to content

Commit c21a92b

Browse files
adding manual DI example
1 parent 5aa8911 commit c21a92b

File tree

8 files changed

+146
-0
lines changed

8 files changed

+146
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package guru.springframework.sfgdi.controllers;
2+
3+
import guru.springframework.sfgdi.services.GreetingService;
4+
5+
/**
6+
* Created by jt on 12/26/19.
7+
*/
8+
public class ConstructorInjectedController {
9+
private final GreetingService greetingService;
10+
11+
public ConstructorInjectedController(GreetingService greetingService) {
12+
this.greetingService = greetingService;
13+
}
14+
15+
public String getGreeting(){
16+
return greetingService.sayGreeting();
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package guru.springframework.sfgdi.controllers;
2+
3+
import guru.springframework.sfgdi.services.GreetingService;
4+
5+
/**
6+
* Created by jt on 12/26/19.
7+
*/
8+
public class PropertyInjectedController {
9+
10+
public GreetingService greetingService;
11+
12+
public String getGreeting(){
13+
return greetingService.sayGreeting();
14+
}
15+
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package guru.springframework.sfgdi.controllers;
2+
3+
import guru.springframework.sfgdi.services.GreetingService;
4+
5+
/**
6+
* Created by jt on 12/26/19.
7+
*/
8+
public class SetterInjectedController {
9+
10+
private GreetingService greetingService;
11+
12+
public void setGreetingService(GreetingService greetingService) {
13+
this.greetingService = greetingService;
14+
}
15+
16+
public String getGreeting(){
17+
return greetingService.sayGreeting();
18+
}
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package guru.springframework.sfgdi.services;
2+
3+
/**
4+
* Created by jt on 12/26/19.
5+
*/
6+
public interface GreetingService {
7+
8+
String sayGreeting();
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package guru.springframework.sfgdi.services;
2+
3+
/**
4+
* Created by jt on 12/26/19.
5+
*/
6+
public class GreetingServiceImpl implements GreetingService {
7+
@Override
8+
public String sayGreeting() {
9+
return "Hello World";
10+
}
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package guru.springframework.sfgdi.controllers;
2+
3+
import guru.springframework.sfgdi.services.GreetingServiceImpl;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class ConstructorInjectedControllerTest {
10+
11+
ConstructorInjectedController controller;
12+
13+
@BeforeEach
14+
void setUp() {
15+
controller = new ConstructorInjectedController(new GreetingServiceImpl());
16+
}
17+
18+
@Test
19+
void getGreeting() {
20+
21+
System.out.println(controller.getGreeting());
22+
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package guru.springframework.sfgdi.controllers;
2+
3+
import guru.springframework.sfgdi.services.GreetingServiceImpl;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class PropertyInjectedControllerTest {
10+
11+
PropertyInjectedController controller;
12+
13+
@BeforeEach
14+
void setUp() {
15+
controller = new PropertyInjectedController();
16+
17+
controller.greetingService = new GreetingServiceImpl();
18+
}
19+
20+
@Test
21+
void getGreeting() {
22+
23+
System.out.println(controller.getGreeting());
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package guru.springframework.sfgdi.controllers;
2+
3+
import guru.springframework.sfgdi.services.GreetingServiceImpl;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class SetterInjectedControllerTest {
10+
11+
SetterInjectedController controller;
12+
13+
@BeforeEach
14+
void setUp() {
15+
controller = new SetterInjectedController();
16+
controller.setGreetingService(new GreetingServiceImpl());
17+
}
18+
19+
@Test
20+
void getGreeting() {
21+
System.out.println(controller.getGreeting());
22+
23+
}
24+
}

0 commit comments

Comments
 (0)