From 76071d5e5b62cb71b5cea9464ae2319dda79db43 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Tue, 23 May 2017 17:02:24 -0400 Subject: [PATCH 01/15] adding context example --- .../guru/springframework/DiDemoApplication.java | 8 +++++++- .../controllers/MyController.java | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/main/java/guru/springframework/controllers/MyController.java diff --git a/src/main/java/guru/springframework/DiDemoApplication.java b/src/main/java/guru/springframework/DiDemoApplication.java index 9e42add3..edb8a7fc 100644 --- a/src/main/java/guru/springframework/DiDemoApplication.java +++ b/src/main/java/guru/springframework/DiDemoApplication.java @@ -1,12 +1,18 @@ package guru.springframework; +import guru.springframework.controllers.MyController; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; @SpringBootApplication public class DiDemoApplication { public static void main(String[] args) { - SpringApplication.run(DiDemoApplication.class, args); + ApplicationContext ctx = SpringApplication.run(DiDemoApplication.class, args); + + MyController controller = (MyController) ctx.getBean("myController"); + + controller.hello(); } } diff --git a/src/main/java/guru/springframework/controllers/MyController.java b/src/main/java/guru/springframework/controllers/MyController.java new file mode 100644 index 00000000..e2fad0e4 --- /dev/null +++ b/src/main/java/guru/springframework/controllers/MyController.java @@ -0,0 +1,16 @@ +package guru.springframework.controllers; + +import org.springframework.stereotype.Controller; + +/** + * Created by jt on 5/23/17. + */ +@Controller +public class MyController { + + public String hello(){ + System.out.println("Hello!!! "); + + return "foo"; + } +} From b2cac1c30089e33f959a82640d7afcd980d29e0d Mon Sep 17 00:00:00 2001 From: John Thompson Date: Wed, 24 May 2017 16:47:44 -0400 Subject: [PATCH 02/15] adding Manual DI Example --- .../ConstructorInjectedController.java | 20 ++++++++++++++ .../controllers/GetterInjectedController.java | 19 ++++++++++++++ .../PropertyInjectedController.java | 18 +++++++++++++ .../services/GreetingService.java | 9 +++++++ .../services/GreetingServiceImpl.java | 17 ++++++++++++ .../ConstructorInjectedControllerTest.java | 24 +++++++++++++++++ .../GetterInjectedControllerTest.java | 26 +++++++++++++++++++ .../PropertyInjectedControllerTest.java | 26 +++++++++++++++++++ 8 files changed, 159 insertions(+) create mode 100644 src/main/java/guru/springframework/controllers/ConstructorInjectedController.java create mode 100644 src/main/java/guru/springframework/controllers/GetterInjectedController.java create mode 100644 src/main/java/guru/springframework/controllers/PropertyInjectedController.java create mode 100644 src/main/java/guru/springframework/services/GreetingService.java create mode 100644 src/main/java/guru/springframework/services/GreetingServiceImpl.java create mode 100644 src/test/java/guru/springframework/controllers/ConstructorInjectedControllerTest.java create mode 100644 src/test/java/guru/springframework/controllers/GetterInjectedControllerTest.java create mode 100644 src/test/java/guru/springframework/controllers/PropertyInjectedControllerTest.java diff --git a/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java b/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java new file mode 100644 index 00000000..f3bcf6cd --- /dev/null +++ b/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java @@ -0,0 +1,20 @@ +package guru.springframework.controllers; + +import guru.springframework.services.GreetingService; +import guru.springframework.services.GreetingServiceImpl; + +/** + * Created by jt on 5/24/17. + */ +public class ConstructorInjectedController { + + private GreetingService greetingService; + + public ConstructorInjectedController(GreetingService greetingService) { + this.greetingService = greetingService; + } + + String sayHello(){ + return greetingService.sayGreeting(); + } +} diff --git a/src/main/java/guru/springframework/controllers/GetterInjectedController.java b/src/main/java/guru/springframework/controllers/GetterInjectedController.java new file mode 100644 index 00000000..2d101437 --- /dev/null +++ b/src/main/java/guru/springframework/controllers/GetterInjectedController.java @@ -0,0 +1,19 @@ +package guru.springframework.controllers; + +import guru.springframework.services.GreetingService; +import guru.springframework.services.GreetingServiceImpl; + +/** + * Created by jt on 5/24/17. + */ +public class GetterInjectedController { + private GreetingService greetingService; + + String sayHello(){ + return greetingService.sayGreeting(); + } + + public void setGreetingService(GreetingService greetingService) { + this.greetingService = greetingService; + } +} diff --git a/src/main/java/guru/springframework/controllers/PropertyInjectedController.java b/src/main/java/guru/springframework/controllers/PropertyInjectedController.java new file mode 100644 index 00000000..b73bdcdd --- /dev/null +++ b/src/main/java/guru/springframework/controllers/PropertyInjectedController.java @@ -0,0 +1,18 @@ +package guru.springframework.controllers; + +import guru.springframework.services.GreetingServiceImpl; + + +/** + * Created by jt on 5/24/17. + */ + +public class PropertyInjectedController { + + public GreetingServiceImpl greetingService; + + String sayHello(){ + return greetingService.sayGreeting(); + } + +} diff --git a/src/main/java/guru/springframework/services/GreetingService.java b/src/main/java/guru/springframework/services/GreetingService.java new file mode 100644 index 00000000..836d17c1 --- /dev/null +++ b/src/main/java/guru/springframework/services/GreetingService.java @@ -0,0 +1,9 @@ +package guru.springframework.services; + +/** + * Created by jt on 5/24/17. + */ +public interface GreetingService { + + String sayGreeting(); +} diff --git a/src/main/java/guru/springframework/services/GreetingServiceImpl.java b/src/main/java/guru/springframework/services/GreetingServiceImpl.java new file mode 100644 index 00000000..b3519d0e --- /dev/null +++ b/src/main/java/guru/springframework/services/GreetingServiceImpl.java @@ -0,0 +1,17 @@ +package guru.springframework.services; + +import org.springframework.stereotype.Service; + +/** + * Created by jt on 5/24/17. + */ +@Service +public class GreetingServiceImpl implements GreetingService { + + public static final String HELLO_GURUS = "Hello Gurus!!!!"; + + @Override + public String sayGreeting() { + return HELLO_GURUS; + } +} diff --git a/src/test/java/guru/springframework/controllers/ConstructorInjectedControllerTest.java b/src/test/java/guru/springframework/controllers/ConstructorInjectedControllerTest.java new file mode 100644 index 00000000..bbf36c6e --- /dev/null +++ b/src/test/java/guru/springframework/controllers/ConstructorInjectedControllerTest.java @@ -0,0 +1,24 @@ +package guru.springframework.controllers; + +import guru.springframework.services.GreetingServiceImpl; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Created by jt on 5/24/17. + */ +public class ConstructorInjectedControllerTest { + private ConstructorInjectedController constructorInjectedController; + + @Before + public void setUp() throws Exception { + this.constructorInjectedController = new ConstructorInjectedController(new GreetingServiceImpl()); + } + + @Test + public void testGreeting() throws Exception { + assertEquals(GreetingServiceImpl.HELLO_GURUS, constructorInjectedController.sayHello()); + } +} \ No newline at end of file diff --git a/src/test/java/guru/springframework/controllers/GetterInjectedControllerTest.java b/src/test/java/guru/springframework/controllers/GetterInjectedControllerTest.java new file mode 100644 index 00000000..f0837998 --- /dev/null +++ b/src/test/java/guru/springframework/controllers/GetterInjectedControllerTest.java @@ -0,0 +1,26 @@ +package guru.springframework.controllers; + +import guru.springframework.services.GreetingServiceImpl; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Created by jt on 5/24/17. + */ +public class GetterInjectedControllerTest { + + private GetterInjectedController getterInjectedController; + + @Before + public void setUp() throws Exception { + this.getterInjectedController = new GetterInjectedController(); + this.getterInjectedController.setGreetingService(new GreetingServiceImpl()); + } + + @Test + public void testGreeting() throws Exception { + assertEquals(GreetingServiceImpl.HELLO_GURUS, getterInjectedController.sayHello()); + } +} \ No newline at end of file diff --git a/src/test/java/guru/springframework/controllers/PropertyInjectedControllerTest.java b/src/test/java/guru/springframework/controllers/PropertyInjectedControllerTest.java new file mode 100644 index 00000000..e91cee57 --- /dev/null +++ b/src/test/java/guru/springframework/controllers/PropertyInjectedControllerTest.java @@ -0,0 +1,26 @@ +package guru.springframework.controllers; + +import guru.springframework.services.GreetingServiceImpl; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Created by jt on 5/24/17. + */ +public class PropertyInjectedControllerTest { + + private PropertyInjectedController propertyInjectedController; + + @Before + public void setUp() throws Exception { + this.propertyInjectedController = new PropertyInjectedController(); + this.propertyInjectedController.greetingService = new GreetingServiceImpl(); + } + + @Test + public void testGreeting() throws Exception { + assertEquals(GreetingServiceImpl.HELLO_GURUS, propertyInjectedController.sayHello()); + } +} \ No newline at end of file From 32c671ff0f8d4ff4e76cfdf994eec0dbf0aab098 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Wed, 24 May 2017 16:55:53 -0400 Subject: [PATCH 03/15] making controller greeting methods public --- .../controllers/ConstructorInjectedController.java | 2 +- .../springframework/controllers/GetterInjectedController.java | 2 +- .../springframework/controllers/PropertyInjectedController.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java b/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java index f3bcf6cd..c689cbbb 100644 --- a/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java +++ b/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java @@ -14,7 +14,7 @@ public ConstructorInjectedController(GreetingService greetingService) { this.greetingService = greetingService; } - String sayHello(){ + public String sayHello(){ return greetingService.sayGreeting(); } } diff --git a/src/main/java/guru/springframework/controllers/GetterInjectedController.java b/src/main/java/guru/springframework/controllers/GetterInjectedController.java index 2d101437..d63685bf 100644 --- a/src/main/java/guru/springframework/controllers/GetterInjectedController.java +++ b/src/main/java/guru/springframework/controllers/GetterInjectedController.java @@ -9,7 +9,7 @@ public class GetterInjectedController { private GreetingService greetingService; - String sayHello(){ + public String sayHello(){ return greetingService.sayGreeting(); } diff --git a/src/main/java/guru/springframework/controllers/PropertyInjectedController.java b/src/main/java/guru/springframework/controllers/PropertyInjectedController.java index b73bdcdd..2f2b6f9e 100644 --- a/src/main/java/guru/springframework/controllers/PropertyInjectedController.java +++ b/src/main/java/guru/springframework/controllers/PropertyInjectedController.java @@ -11,7 +11,7 @@ public class PropertyInjectedController { public GreetingServiceImpl greetingService; - String sayHello(){ + public String sayHello(){ return greetingService.sayGreeting(); } From 29ca0a5af56b35cd405ea27db1a4e3b620ddf264 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Wed, 24 May 2017 17:13:03 -0400 Subject: [PATCH 04/15] making controller greeting methods public --- src/main/java/guru/springframework/DiDemoApplication.java | 7 +++++++ .../controllers/ConstructorInjectedController.java | 4 ++++ .../controllers/GetterInjectedController.java | 4 ++++ .../controllers/PropertyInjectedController.java | 5 ++++- 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/guru/springframework/DiDemoApplication.java b/src/main/java/guru/springframework/DiDemoApplication.java index edb8a7fc..ad23374a 100644 --- a/src/main/java/guru/springframework/DiDemoApplication.java +++ b/src/main/java/guru/springframework/DiDemoApplication.java @@ -1,6 +1,9 @@ package guru.springframework; +import guru.springframework.controllers.ConstructorInjectedController; +import guru.springframework.controllers.GetterInjectedController; import guru.springframework.controllers.MyController; +import guru.springframework.controllers.PropertyInjectedController; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; @@ -14,5 +17,9 @@ public static void main(String[] args) { MyController controller = (MyController) ctx.getBean("myController"); controller.hello(); + + System.out.println(ctx.getBean(PropertyInjectedController.class).sayHello()); + System.out.println(ctx.getBean(GetterInjectedController.class).sayHello()); + System.out.println(ctx.getBean(ConstructorInjectedController.class).sayHello()); } } diff --git a/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java b/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java index c689cbbb..a40acab4 100644 --- a/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java +++ b/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java @@ -2,14 +2,18 @@ import guru.springframework.services.GreetingService; import guru.springframework.services.GreetingServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; /** * Created by jt on 5/24/17. */ +@Controller public class ConstructorInjectedController { private GreetingService greetingService; + @Autowired public ConstructorInjectedController(GreetingService greetingService) { this.greetingService = greetingService; } diff --git a/src/main/java/guru/springframework/controllers/GetterInjectedController.java b/src/main/java/guru/springframework/controllers/GetterInjectedController.java index d63685bf..994a6433 100644 --- a/src/main/java/guru/springframework/controllers/GetterInjectedController.java +++ b/src/main/java/guru/springframework/controllers/GetterInjectedController.java @@ -2,10 +2,13 @@ import guru.springframework.services.GreetingService; import guru.springframework.services.GreetingServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; /** * Created by jt on 5/24/17. */ +@Controller public class GetterInjectedController { private GreetingService greetingService; @@ -13,6 +16,7 @@ public String sayHello(){ return greetingService.sayGreeting(); } + @Autowired public void setGreetingService(GreetingService greetingService) { this.greetingService = greetingService; } diff --git a/src/main/java/guru/springframework/controllers/PropertyInjectedController.java b/src/main/java/guru/springframework/controllers/PropertyInjectedController.java index 2f2b6f9e..067b15dd 100644 --- a/src/main/java/guru/springframework/controllers/PropertyInjectedController.java +++ b/src/main/java/guru/springframework/controllers/PropertyInjectedController.java @@ -1,14 +1,17 @@ package guru.springframework.controllers; import guru.springframework.services.GreetingServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; /** * Created by jt on 5/24/17. */ - +@Controller public class PropertyInjectedController { + @Autowired public GreetingServiceImpl greetingService; public String sayHello(){ From 69f7c68d6fd60e997dfa6fd18709b9f52efbd76c Mon Sep 17 00:00:00 2001 From: John Thompson Date: Wed, 24 May 2017 17:45:49 -0400 Subject: [PATCH 05/15] adding more services for Qualifier example --- .../ConstructorInjectedController.java | 6 ++---- .../controllers/GetterInjectedController.java | 3 ++- .../controllers/PropertyInjectedController.java | 5 +++-- .../services/ConstructorGreetingService.java | 14 ++++++++++++++ .../services/GetterGreetingService.java | 15 +++++++++++++++ .../PropertyInjectedControllerTest.java | 2 +- 6 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 src/main/java/guru/springframework/services/ConstructorGreetingService.java create mode 100644 src/main/java/guru/springframework/services/GetterGreetingService.java diff --git a/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java b/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java index a40acab4..d21186fb 100644 --- a/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java +++ b/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java @@ -1,8 +1,7 @@ package guru.springframework.controllers; import guru.springframework.services.GreetingService; -import guru.springframework.services.GreetingServiceImpl; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; /** @@ -13,8 +12,7 @@ public class ConstructorInjectedController { private GreetingService greetingService; - @Autowired - public ConstructorInjectedController(GreetingService greetingService) { + public ConstructorInjectedController(@Qualifier("constructorGreetingService") GreetingService greetingService) { this.greetingService = greetingService; } diff --git a/src/main/java/guru/springframework/controllers/GetterInjectedController.java b/src/main/java/guru/springframework/controllers/GetterInjectedController.java index 994a6433..52fdc1c9 100644 --- a/src/main/java/guru/springframework/controllers/GetterInjectedController.java +++ b/src/main/java/guru/springframework/controllers/GetterInjectedController.java @@ -3,6 +3,7 @@ import guru.springframework.services.GreetingService; import guru.springframework.services.GreetingServiceImpl; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; /** @@ -17,7 +18,7 @@ public String sayHello(){ } @Autowired - public void setGreetingService(GreetingService greetingService) { + public void setGreetingService(@Qualifier("getterGreetingService") GreetingService greetingService) { this.greetingService = greetingService; } } diff --git a/src/main/java/guru/springframework/controllers/PropertyInjectedController.java b/src/main/java/guru/springframework/controllers/PropertyInjectedController.java index 067b15dd..9cdc1dc6 100644 --- a/src/main/java/guru/springframework/controllers/PropertyInjectedController.java +++ b/src/main/java/guru/springframework/controllers/PropertyInjectedController.java @@ -1,5 +1,6 @@ package guru.springframework.controllers; +import guru.springframework.services.GreetingService; import guru.springframework.services.GreetingServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -12,10 +13,10 @@ public class PropertyInjectedController { @Autowired - public GreetingServiceImpl greetingService; + public GreetingService greetingServiceImpl; public String sayHello(){ - return greetingService.sayGreeting(); + return greetingServiceImpl.sayGreeting(); } } diff --git a/src/main/java/guru/springframework/services/ConstructorGreetingService.java b/src/main/java/guru/springframework/services/ConstructorGreetingService.java new file mode 100644 index 00000000..5dc84ded --- /dev/null +++ b/src/main/java/guru/springframework/services/ConstructorGreetingService.java @@ -0,0 +1,14 @@ +package guru.springframework.services; + +import org.springframework.stereotype.Service; + +/** + * Created by jt on 5/24/17. + */ +@Service +public class ConstructorGreetingService implements GreetingService { + @Override + public String sayGreeting() { + return "Hello - I was injected via the constructor!!!"; + } +} diff --git a/src/main/java/guru/springframework/services/GetterGreetingService.java b/src/main/java/guru/springframework/services/GetterGreetingService.java new file mode 100644 index 00000000..922c130a --- /dev/null +++ b/src/main/java/guru/springframework/services/GetterGreetingService.java @@ -0,0 +1,15 @@ +package guru.springframework.services; + +import org.springframework.stereotype.Service; + +/** + * Created by jt on 5/24/17. + */ +@Service +public class GetterGreetingService implements GreetingService { + + @Override + public String sayGreeting() { + return "Hello - I was injected by the getter"; + } +} diff --git a/src/test/java/guru/springframework/controllers/PropertyInjectedControllerTest.java b/src/test/java/guru/springframework/controllers/PropertyInjectedControllerTest.java index e91cee57..e5e98f3d 100644 --- a/src/test/java/guru/springframework/controllers/PropertyInjectedControllerTest.java +++ b/src/test/java/guru/springframework/controllers/PropertyInjectedControllerTest.java @@ -16,7 +16,7 @@ public class PropertyInjectedControllerTest { @Before public void setUp() throws Exception { this.propertyInjectedController = new PropertyInjectedController(); - this.propertyInjectedController.greetingService = new GreetingServiceImpl(); + this.propertyInjectedController.greetingServiceImpl = new GreetingServiceImpl(); } @Test From d96580a71c5774f45e29c13134421022d20f6943 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Wed, 24 May 2017 17:46:32 -0400 Subject: [PATCH 06/15] removing unused imports --- .../springframework/controllers/GetterInjectedController.java | 1 - .../springframework/controllers/PropertyInjectedController.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/java/guru/springframework/controllers/GetterInjectedController.java b/src/main/java/guru/springframework/controllers/GetterInjectedController.java index 52fdc1c9..39c3f3a7 100644 --- a/src/main/java/guru/springframework/controllers/GetterInjectedController.java +++ b/src/main/java/guru/springframework/controllers/GetterInjectedController.java @@ -1,7 +1,6 @@ package guru.springframework.controllers; import guru.springframework.services.GreetingService; -import guru.springframework.services.GreetingServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; diff --git a/src/main/java/guru/springframework/controllers/PropertyInjectedController.java b/src/main/java/guru/springframework/controllers/PropertyInjectedController.java index 9cdc1dc6..33bf4dd5 100644 --- a/src/main/java/guru/springframework/controllers/PropertyInjectedController.java +++ b/src/main/java/guru/springframework/controllers/PropertyInjectedController.java @@ -1,7 +1,6 @@ package guru.springframework.controllers; import guru.springframework.services.GreetingService; -import guru.springframework.services.GreetingServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; From fc7db2a9b8d3b0e96be7ae944f08294d34c819e6 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Wed, 24 May 2017 18:19:16 -0400 Subject: [PATCH 07/15] adding Primary Bean demo --- .../guru/springframework/DiDemoApplication.java | 3 +-- .../controllers/MyController.java | 9 ++++++++- .../controllers/PropertyInjectedController.java | 2 ++ .../services/GreetingServiceImpl.java | 2 +- .../services/PrimaryGreetingService.java | 17 +++++++++++++++++ 5 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 src/main/java/guru/springframework/services/PrimaryGreetingService.java diff --git a/src/main/java/guru/springframework/DiDemoApplication.java b/src/main/java/guru/springframework/DiDemoApplication.java index ad23374a..5e1cc584 100644 --- a/src/main/java/guru/springframework/DiDemoApplication.java +++ b/src/main/java/guru/springframework/DiDemoApplication.java @@ -16,8 +16,7 @@ public static void main(String[] args) { MyController controller = (MyController) ctx.getBean("myController"); - controller.hello(); - + System.out.println(controller.hello()); System.out.println(ctx.getBean(PropertyInjectedController.class).sayHello()); System.out.println(ctx.getBean(GetterInjectedController.class).sayHello()); System.out.println(ctx.getBean(ConstructorInjectedController.class).sayHello()); diff --git a/src/main/java/guru/springframework/controllers/MyController.java b/src/main/java/guru/springframework/controllers/MyController.java index e2fad0e4..683dbb99 100644 --- a/src/main/java/guru/springframework/controllers/MyController.java +++ b/src/main/java/guru/springframework/controllers/MyController.java @@ -1,5 +1,6 @@ package guru.springframework.controllers; +import guru.springframework.services.GreetingService; import org.springframework.stereotype.Controller; /** @@ -8,9 +9,15 @@ @Controller public class MyController { + private GreetingService greetingService; + + public MyController(GreetingService greetingService) { + this.greetingService = greetingService; + } + public String hello(){ System.out.println("Hello!!! "); - return "foo"; + return greetingService.sayGreeting(); } } diff --git a/src/main/java/guru/springframework/controllers/PropertyInjectedController.java b/src/main/java/guru/springframework/controllers/PropertyInjectedController.java index 33bf4dd5..5ffddca4 100644 --- a/src/main/java/guru/springframework/controllers/PropertyInjectedController.java +++ b/src/main/java/guru/springframework/controllers/PropertyInjectedController.java @@ -2,6 +2,7 @@ import guru.springframework.services.GreetingService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; @@ -12,6 +13,7 @@ public class PropertyInjectedController { @Autowired + @Qualifier("greetingServiceImpl") public GreetingService greetingServiceImpl; public String sayHello(){ diff --git a/src/main/java/guru/springframework/services/GreetingServiceImpl.java b/src/main/java/guru/springframework/services/GreetingServiceImpl.java index b3519d0e..4d169c20 100644 --- a/src/main/java/guru/springframework/services/GreetingServiceImpl.java +++ b/src/main/java/guru/springframework/services/GreetingServiceImpl.java @@ -8,7 +8,7 @@ @Service public class GreetingServiceImpl implements GreetingService { - public static final String HELLO_GURUS = "Hello Gurus!!!!"; + public static final String HELLO_GURUS = "Hello Gurus!!!! - Original"; @Override public String sayGreeting() { diff --git a/src/main/java/guru/springframework/services/PrimaryGreetingService.java b/src/main/java/guru/springframework/services/PrimaryGreetingService.java new file mode 100644 index 00000000..f9875e37 --- /dev/null +++ b/src/main/java/guru/springframework/services/PrimaryGreetingService.java @@ -0,0 +1,17 @@ +package guru.springframework.services; + +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Service; + +/** + * Created by jt on 5/24/17. + */ +@Service +@Primary +public class PrimaryGreetingService implements GreetingService { + + @Override + public String sayGreeting() { + return "Hello - Primary Greeting service"; + } +} From bbb782f9a8a4c7d96396bc3d44314f6c57eba568 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Wed, 24 May 2017 18:37:38 -0400 Subject: [PATCH 08/15] adding Primary Bean demo --- .../services/PrimaryGreetingService.java | 2 ++ .../PrimarySpanishGreetingService.java | 19 +++++++++++++++++++ src/main/resources/application.properties | 1 + 3 files changed, 22 insertions(+) create mode 100644 src/main/java/guru/springframework/services/PrimarySpanishGreetingService.java diff --git a/src/main/java/guru/springframework/services/PrimaryGreetingService.java b/src/main/java/guru/springframework/services/PrimaryGreetingService.java index f9875e37..714f416a 100644 --- a/src/main/java/guru/springframework/services/PrimaryGreetingService.java +++ b/src/main/java/guru/springframework/services/PrimaryGreetingService.java @@ -1,6 +1,7 @@ package guru.springframework.services; import org.springframework.context.annotation.Primary; +import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; /** @@ -8,6 +9,7 @@ */ @Service @Primary +@Profile("en") public class PrimaryGreetingService implements GreetingService { @Override diff --git a/src/main/java/guru/springframework/services/PrimarySpanishGreetingService.java b/src/main/java/guru/springframework/services/PrimarySpanishGreetingService.java new file mode 100644 index 00000000..6758af11 --- /dev/null +++ b/src/main/java/guru/springframework/services/PrimarySpanishGreetingService.java @@ -0,0 +1,19 @@ +package guru.springframework.services; + +import org.springframework.context.annotation.Primary; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Service; + +/** + * Created by jt on 5/24/17. + */ +@Service +@Profile("es") +@Primary +public class PrimarySpanishGreetingService implements GreetingService { + + @Override + public String sayGreeting() { + return "Servicio de Saludo Primario"; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29b..51ef3d0a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.profiles.active=es \ No newline at end of file From f84eaea43112f15c70d82d6ee414bd48ae7f5191 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Wed, 24 May 2017 18:58:39 -0400 Subject: [PATCH 09/15] adding default profile demo --- .../services/PrimaryGermanGreetingService.java | 18 ++++++++++++++++++ .../services/PrimaryGreetingService.java | 2 +- src/main/resources/application.properties | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 src/main/java/guru/springframework/services/PrimaryGermanGreetingService.java diff --git a/src/main/java/guru/springframework/services/PrimaryGermanGreetingService.java b/src/main/java/guru/springframework/services/PrimaryGermanGreetingService.java new file mode 100644 index 00000000..acdadade --- /dev/null +++ b/src/main/java/guru/springframework/services/PrimaryGermanGreetingService.java @@ -0,0 +1,18 @@ +package guru.springframework.services; + +import org.springframework.context.annotation.Primary; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Service; + +/** + * Created by jt on 5/24/17. + */ +@Service +@Primary +@Profile("de") +public class PrimaryGermanGreetingService implements GreetingService { + @Override + public String sayGreeting() { + return "Primärer Grußdienst"; + } +} diff --git a/src/main/java/guru/springframework/services/PrimaryGreetingService.java b/src/main/java/guru/springframework/services/PrimaryGreetingService.java index 714f416a..233034a2 100644 --- a/src/main/java/guru/springframework/services/PrimaryGreetingService.java +++ b/src/main/java/guru/springframework/services/PrimaryGreetingService.java @@ -9,7 +9,7 @@ */ @Service @Primary -@Profile("en") +@Profile({"en", "default"}) public class PrimaryGreetingService implements GreetingService { @Override diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 51ef3d0a..55a593d3 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1 @@ -spring.profiles.active=es \ No newline at end of file +spring.profiles.active=de \ No newline at end of file From b22f58ef09fe4b1593902d45056f154e97c42ed8 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Wed, 24 May 2017 19:14:39 -0400 Subject: [PATCH 10/15] adding default profile demo --- .../services/GreetingRepository.java | 13 ++++++++++ .../services/GreetingRepositoryImpl.java | 24 +++++++++++++++++++ .../PrimaryGermanGreetingService.java | 5 +++- .../services/PrimaryGreetingService.java | 4 +++- .../PrimarySpanishGreetingService.java | 4 +++- 5 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 src/main/java/guru/springframework/services/GreetingRepository.java create mode 100644 src/main/java/guru/springframework/services/GreetingRepositoryImpl.java diff --git a/src/main/java/guru/springframework/services/GreetingRepository.java b/src/main/java/guru/springframework/services/GreetingRepository.java new file mode 100644 index 00000000..59f38293 --- /dev/null +++ b/src/main/java/guru/springframework/services/GreetingRepository.java @@ -0,0 +1,13 @@ +package guru.springframework.services; + +/** + * Created by jt on 5/24/17. + */ +public interface GreetingRepository { + + String getEnglishGreeting(); + + String getSpanishGreeting(); + + String getGermanGreeting(); +} diff --git a/src/main/java/guru/springframework/services/GreetingRepositoryImpl.java b/src/main/java/guru/springframework/services/GreetingRepositoryImpl.java new file mode 100644 index 00000000..b56b4911 --- /dev/null +++ b/src/main/java/guru/springframework/services/GreetingRepositoryImpl.java @@ -0,0 +1,24 @@ +package guru.springframework.services; + +/** + * Created by jt on 5/24/17. + */ +public class GreetingRepositoryImpl implements GreetingRepository { + + @Override + public String getEnglishGreeting() { + return "Hello - Primary Greeting service"; + } + + @Override + public String getSpanishGreeting() { + return "Servicio de Saludo Primario"; + } + + @Override + public String getGermanGreeting() { + + return "Primärer Grußdienst"; + + } +} diff --git a/src/main/java/guru/springframework/services/PrimaryGermanGreetingService.java b/src/main/java/guru/springframework/services/PrimaryGermanGreetingService.java index acdadade..5373f440 100644 --- a/src/main/java/guru/springframework/services/PrimaryGermanGreetingService.java +++ b/src/main/java/guru/springframework/services/PrimaryGermanGreetingService.java @@ -11,8 +11,11 @@ @Primary @Profile("de") public class PrimaryGermanGreetingService implements GreetingService { + + private GreetingRepository greetingRepository; + @Override public String sayGreeting() { - return "Primärer Grußdienst"; + return greetingRepository.getGermanGreeting(); } } diff --git a/src/main/java/guru/springframework/services/PrimaryGreetingService.java b/src/main/java/guru/springframework/services/PrimaryGreetingService.java index 233034a2..6b99b856 100644 --- a/src/main/java/guru/springframework/services/PrimaryGreetingService.java +++ b/src/main/java/guru/springframework/services/PrimaryGreetingService.java @@ -12,8 +12,10 @@ @Profile({"en", "default"}) public class PrimaryGreetingService implements GreetingService { + private GreetingRepository greetingRepository; + @Override public String sayGreeting() { - return "Hello - Primary Greeting service"; + return greetingRepository.getEnglishGreeting(); } } diff --git a/src/main/java/guru/springframework/services/PrimarySpanishGreetingService.java b/src/main/java/guru/springframework/services/PrimarySpanishGreetingService.java index 6758af11..67c0d623 100644 --- a/src/main/java/guru/springframework/services/PrimarySpanishGreetingService.java +++ b/src/main/java/guru/springframework/services/PrimarySpanishGreetingService.java @@ -12,8 +12,10 @@ @Primary public class PrimarySpanishGreetingService implements GreetingService { + private GreetingRepository greetingRepository; + @Override public String sayGreeting() { - return "Servicio de Saludo Primario"; + return greetingRepository.getSpanishGreeting(); } } From f7e85057098a85457dc9399677a8bdc3e6401833 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Mon, 19 Jun 2017 13:41:04 -0400 Subject: [PATCH 11/15] updating Spring Boot version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index e2c6a36b..c2bce693 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M1 + 2.0.0.M2 From fc714ae007c5b315d5ba1352875b6bd68bff0073 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Mon, 31 Jul 2017 16:57:11 -0400 Subject: [PATCH 12/15] Updating Spring Boot Version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index c2bce693..5dde83a4 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M2 + 2.0.0.M3 From 610afded088441ccd2ba5d8a39c4f3b9b4c804cf Mon Sep 17 00:00:00 2001 From: springframeworkguru Date: Sun, 10 Dec 2017 09:05:32 +0530 Subject: [PATCH 13/15] Updated to 2.0.0.M7 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 5dde83a4..351d95af 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M3 + 2.0.0.M7 From 8dd6205a174cec427727e99595dd5beb1675dd50 Mon Sep 17 00:00:00 2001 From: springframeworkguru Date: Sun, 18 Mar 2018 07:15:56 +0530 Subject: [PATCH 14/15] Updated to Spring Boot 2.0.0.RELEASE --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 351d95af..eb823c68 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M7 + 2.0.0.RELEASE From 5b86a4be932234758d330cf12c9f5bdb266d7f57 Mon Sep 17 00:00:00 2001 From: springframeworkguru Date: Wed, 5 Dec 2018 01:49:53 +0530 Subject: [PATCH 15/15] Upgraded to Spring Boot 2.1.0.RELEASE --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index eb823c68..1de0549d 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.RELEASE + 2.1.0.RELEASE