From c21a92bfb4780fc0d20ebbf370aacf250765bd51 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Thu, 26 Dec 2019 16:17:28 -0500 Subject: [PATCH] adding manual DI example --- .../ConstructorInjectedController.java | 18 +++++++++++++ .../PropertyInjectedController.java | 16 ++++++++++++ .../controllers/SetterInjectedController.java | 19 ++++++++++++++ .../sfgdi/services/GreetingService.java | 9 +++++++ .../sfgdi/services/GreetingServiceImpl.java | 11 ++++++++ .../ConstructorInjectedControllerTest.java | 24 ++++++++++++++++++ .../PropertyInjectedControllerTest.java | 25 +++++++++++++++++++ .../SetterInjectedControllerTest.java | 24 ++++++++++++++++++ 8 files changed, 146 insertions(+) create mode 100644 src/main/java/guru/springframework/sfgdi/controllers/ConstructorInjectedController.java create mode 100644 src/main/java/guru/springframework/sfgdi/controllers/PropertyInjectedController.java create mode 100644 src/main/java/guru/springframework/sfgdi/controllers/SetterInjectedController.java create mode 100644 src/main/java/guru/springframework/sfgdi/services/GreetingService.java create mode 100644 src/main/java/guru/springframework/sfgdi/services/GreetingServiceImpl.java create mode 100644 src/test/java/guru/springframework/sfgdi/controllers/ConstructorInjectedControllerTest.java create mode 100644 src/test/java/guru/springframework/sfgdi/controllers/PropertyInjectedControllerTest.java create mode 100644 src/test/java/guru/springframework/sfgdi/controllers/SetterInjectedControllerTest.java diff --git a/src/main/java/guru/springframework/sfgdi/controllers/ConstructorInjectedController.java b/src/main/java/guru/springframework/sfgdi/controllers/ConstructorInjectedController.java new file mode 100644 index 0000000000..ee6ca4330c --- /dev/null +++ b/src/main/java/guru/springframework/sfgdi/controllers/ConstructorInjectedController.java @@ -0,0 +1,18 @@ +package guru.springframework.sfgdi.controllers; + +import guru.springframework.sfgdi.services.GreetingService; + +/** + * Created by jt on 12/26/19. + */ +public class ConstructorInjectedController { + private final GreetingService greetingService; + + public ConstructorInjectedController(GreetingService greetingService) { + this.greetingService = greetingService; + } + + public String getGreeting(){ + return greetingService.sayGreeting(); + } +} diff --git a/src/main/java/guru/springframework/sfgdi/controllers/PropertyInjectedController.java b/src/main/java/guru/springframework/sfgdi/controllers/PropertyInjectedController.java new file mode 100644 index 0000000000..55bbce5095 --- /dev/null +++ b/src/main/java/guru/springframework/sfgdi/controllers/PropertyInjectedController.java @@ -0,0 +1,16 @@ +package guru.springframework.sfgdi.controllers; + +import guru.springframework.sfgdi.services.GreetingService; + +/** + * Created by jt on 12/26/19. + */ +public class PropertyInjectedController { + + public GreetingService greetingService; + + public String getGreeting(){ + return greetingService.sayGreeting(); + } + +} diff --git a/src/main/java/guru/springframework/sfgdi/controllers/SetterInjectedController.java b/src/main/java/guru/springframework/sfgdi/controllers/SetterInjectedController.java new file mode 100644 index 0000000000..2aedc9a7d0 --- /dev/null +++ b/src/main/java/guru/springframework/sfgdi/controllers/SetterInjectedController.java @@ -0,0 +1,19 @@ +package guru.springframework.sfgdi.controllers; + +import guru.springframework.sfgdi.services.GreetingService; + +/** + * Created by jt on 12/26/19. + */ +public class SetterInjectedController { + + private GreetingService greetingService; + + public void setGreetingService(GreetingService greetingService) { + this.greetingService = greetingService; + } + + public String getGreeting(){ + return greetingService.sayGreeting(); + } +} diff --git a/src/main/java/guru/springframework/sfgdi/services/GreetingService.java b/src/main/java/guru/springframework/sfgdi/services/GreetingService.java new file mode 100644 index 0000000000..1fe13dcfd9 --- /dev/null +++ b/src/main/java/guru/springframework/sfgdi/services/GreetingService.java @@ -0,0 +1,9 @@ +package guru.springframework.sfgdi.services; + +/** + * Created by jt on 12/26/19. + */ +public interface GreetingService { + + String sayGreeting(); +} diff --git a/src/main/java/guru/springframework/sfgdi/services/GreetingServiceImpl.java b/src/main/java/guru/springframework/sfgdi/services/GreetingServiceImpl.java new file mode 100644 index 0000000000..081658c73e --- /dev/null +++ b/src/main/java/guru/springframework/sfgdi/services/GreetingServiceImpl.java @@ -0,0 +1,11 @@ +package guru.springframework.sfgdi.services; + +/** + * Created by jt on 12/26/19. + */ +public class GreetingServiceImpl implements GreetingService { + @Override + public String sayGreeting() { + return "Hello World"; + } +} diff --git a/src/test/java/guru/springframework/sfgdi/controllers/ConstructorInjectedControllerTest.java b/src/test/java/guru/springframework/sfgdi/controllers/ConstructorInjectedControllerTest.java new file mode 100644 index 0000000000..c5c0f9d200 --- /dev/null +++ b/src/test/java/guru/springframework/sfgdi/controllers/ConstructorInjectedControllerTest.java @@ -0,0 +1,24 @@ +package guru.springframework.sfgdi.controllers; + +import guru.springframework.sfgdi.services.GreetingServiceImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ConstructorInjectedControllerTest { + + ConstructorInjectedController controller; + + @BeforeEach + void setUp() { + controller = new ConstructorInjectedController(new GreetingServiceImpl()); + } + + @Test + void getGreeting() { + + System.out.println(controller.getGreeting()); + + } +} \ No newline at end of file diff --git a/src/test/java/guru/springframework/sfgdi/controllers/PropertyInjectedControllerTest.java b/src/test/java/guru/springframework/sfgdi/controllers/PropertyInjectedControllerTest.java new file mode 100644 index 0000000000..7a586e515b --- /dev/null +++ b/src/test/java/guru/springframework/sfgdi/controllers/PropertyInjectedControllerTest.java @@ -0,0 +1,25 @@ +package guru.springframework.sfgdi.controllers; + +import guru.springframework.sfgdi.services.GreetingServiceImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class PropertyInjectedControllerTest { + + PropertyInjectedController controller; + + @BeforeEach + void setUp() { + controller = new PropertyInjectedController(); + + controller.greetingService = new GreetingServiceImpl(); + } + + @Test + void getGreeting() { + + System.out.println(controller.getGreeting()); + } +} \ No newline at end of file diff --git a/src/test/java/guru/springframework/sfgdi/controllers/SetterInjectedControllerTest.java b/src/test/java/guru/springframework/sfgdi/controllers/SetterInjectedControllerTest.java new file mode 100644 index 0000000000..c5c113dfdd --- /dev/null +++ b/src/test/java/guru/springframework/sfgdi/controllers/SetterInjectedControllerTest.java @@ -0,0 +1,24 @@ +package guru.springframework.sfgdi.controllers; + +import guru.springframework.sfgdi.services.GreetingServiceImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class SetterInjectedControllerTest { + + SetterInjectedController controller; + + @BeforeEach + void setUp() { + controller = new SetterInjectedController(); + controller.setGreetingService(new GreetingServiceImpl()); + } + + @Test + void getGreeting() { + System.out.println(controller.getGreeting()); + + } +} \ No newline at end of file