diff --git a/src/main/java/guru/springframework/sfgdi/controllers/ConstructorInjectedController.java b/src/main/java/guru/springframework/sfgdi/controllers/ConstructorInjectedController.java index 42afc32977..a09934a4ba 100644 --- a/src/main/java/guru/springframework/sfgdi/controllers/ConstructorInjectedController.java +++ b/src/main/java/guru/springframework/sfgdi/controllers/ConstructorInjectedController.java @@ -1,6 +1,7 @@ package guru.springframework.sfgdi.controllers; import guru.springframework.sfgdi.services.GreetingService; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; @Controller @@ -8,7 +9,7 @@ public class ConstructorInjectedController { private final GreetingService greetingService; - public ConstructorInjectedController(GreetingService greetingService) { + public ConstructorInjectedController(@Qualifier("constructorGreetingService") GreetingService greetingService) { this.greetingService = greetingService; } diff --git a/src/main/java/guru/springframework/sfgdi/controllers/PropertyInjectedController.java b/src/main/java/guru/springframework/sfgdi/controllers/PropertyInjectedController.java index f3ee00fb04..cc95f1d896 100644 --- a/src/main/java/guru/springframework/sfgdi/controllers/PropertyInjectedController.java +++ b/src/main/java/guru/springframework/sfgdi/controllers/PropertyInjectedController.java @@ -2,12 +2,14 @@ import guru.springframework.sfgdi.services.GreetingService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; @Controller public class PropertyInjectedController { @Autowired + @Qualifier("propertyInjectedGreetingService") public GreetingService greetingService; public String getGretting() { diff --git a/src/main/java/guru/springframework/sfgdi/controllers/SetterInjectedController.java b/src/main/java/guru/springframework/sfgdi/controllers/SetterInjectedController.java index 99a609fa10..74c2b7a150 100644 --- a/src/main/java/guru/springframework/sfgdi/controllers/SetterInjectedController.java +++ b/src/main/java/guru/springframework/sfgdi/controllers/SetterInjectedController.java @@ -2,6 +2,7 @@ import guru.springframework.sfgdi.services.GreetingService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; @Controller @@ -11,6 +12,7 @@ public class SetterInjectedController { private GreetingService greetingService; @Autowired + @Qualifier("setterInjectedGreetingService") public void setGreetingService(GreetingService greetingService) { this.greetingService = greetingService; } diff --git a/src/main/java/guru/springframework/sfgdi/services/ConstructorGreetingService.java b/src/main/java/guru/springframework/sfgdi/services/ConstructorGreetingService.java new file mode 100644 index 0000000000..ef6ca5dfaa --- /dev/null +++ b/src/main/java/guru/springframework/sfgdi/services/ConstructorGreetingService.java @@ -0,0 +1,11 @@ +package guru.springframework.sfgdi.services; + +import org.springframework.stereotype.Service; + +@Service +public class ConstructorGreetingService implements GreetingService { + @Override + public String sayGreeting() { + return "Hello World! - Constructor"; + } +} diff --git a/src/main/java/guru/springframework/sfgdi/services/PropertyInjectedGreetingService.java b/src/main/java/guru/springframework/sfgdi/services/PropertyInjectedGreetingService.java new file mode 100644 index 0000000000..f3f6773ce6 --- /dev/null +++ b/src/main/java/guru/springframework/sfgdi/services/PropertyInjectedGreetingService.java @@ -0,0 +1,11 @@ +package guru.springframework.sfgdi.services; + +import org.springframework.stereotype.Service; + +@Service +public class PropertyInjectedGreetingService implements GreetingService { + @Override + public String sayGreeting() { + return "Hello World! - Property"; + } +} diff --git a/src/main/java/guru/springframework/sfgdi/services/GreetingServiceImpl.java b/src/main/java/guru/springframework/sfgdi/services/SetterInjectedGreetingService.java similarity index 58% rename from src/main/java/guru/springframework/sfgdi/services/GreetingServiceImpl.java rename to src/main/java/guru/springframework/sfgdi/services/SetterInjectedGreetingService.java index 3eab8be6d5..36e76dd902 100644 --- a/src/main/java/guru/springframework/sfgdi/services/GreetingServiceImpl.java +++ b/src/main/java/guru/springframework/sfgdi/services/SetterInjectedGreetingService.java @@ -3,9 +3,9 @@ import org.springframework.stereotype.Service; @Service -public class GreetingServiceImpl implements GreetingService { +public class SetterInjectedGreetingService implements GreetingService { @Override public String sayGreeting() { - return "Hello World!"; + return "Hello World! - Setter"; } } diff --git a/src/test/java/guru/springframework/sfgdi/controllers/ConstructorInjectedControllerTest.java b/src/test/java/guru/springframework/sfgdi/controllers/ConstructorInjectedControllerTest.java index 1123471b82..1878c78ae7 100644 --- a/src/test/java/guru/springframework/sfgdi/controllers/ConstructorInjectedControllerTest.java +++ b/src/test/java/guru/springframework/sfgdi/controllers/ConstructorInjectedControllerTest.java @@ -1,18 +1,16 @@ package guru.springframework.sfgdi.controllers; -import guru.springframework.sfgdi.services.GreetingServiceImpl; +import guru.springframework.sfgdi.services.ConstructorGreetingService; 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()); + controller = new ConstructorInjectedController(new ConstructorGreetingService()); } @Test diff --git a/src/test/java/guru/springframework/sfgdi/controllers/PropertyInjectedControllerTest.java b/src/test/java/guru/springframework/sfgdi/controllers/PropertyInjectedControllerTest.java index e5c8304b51..c4d8591744 100644 --- a/src/test/java/guru/springframework/sfgdi/controllers/PropertyInjectedControllerTest.java +++ b/src/test/java/guru/springframework/sfgdi/controllers/PropertyInjectedControllerTest.java @@ -1,11 +1,9 @@ package guru.springframework.sfgdi.controllers; -import guru.springframework.sfgdi.services.GreetingServiceImpl; +import guru.springframework.sfgdi.services.ConstructorGreetingService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - class PropertyInjectedControllerTest { PropertyInjectedController controller; @@ -14,7 +12,7 @@ class PropertyInjectedControllerTest { void setUp() { controller = new PropertyInjectedController(); - controller.greetingService = new GreetingServiceImpl(); + controller.greetingService = new ConstructorGreetingService(); } @Test diff --git a/src/test/java/guru/springframework/sfgdi/controllers/SetterInjectedControllerTest.java b/src/test/java/guru/springframework/sfgdi/controllers/SetterInjectedControllerTest.java index 556cffad1f..0f6c0e8d7e 100644 --- a/src/test/java/guru/springframework/sfgdi/controllers/SetterInjectedControllerTest.java +++ b/src/test/java/guru/springframework/sfgdi/controllers/SetterInjectedControllerTest.java @@ -1,11 +1,9 @@ package guru.springframework.sfgdi.controllers; -import guru.springframework.sfgdi.services.GreetingServiceImpl; +import guru.springframework.sfgdi.services.ConstructorGreetingService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - class SetterInjectedControllerTest { SetterInjectedController controller; @@ -13,7 +11,7 @@ class SetterInjectedControllerTest { @BeforeEach void setUp() { controller = new SetterInjectedController(); - controller.setGreetingService(new GreetingServiceImpl()); + controller.setGreetingService(new ConstructorGreetingService()); } @Test