diff --git a/pom.xml b/pom.xml index e2c6a36b..1de0549d 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M1 + 2.1.0.RELEASE diff --git a/src/main/java/guru/springframework/DiDemoApplication.java b/src/main/java/guru/springframework/DiDemoApplication.java index 9e42add3..ad23374a 100644 --- a/src/main/java/guru/springframework/DiDemoApplication.java +++ b/src/main/java/guru/springframework/DiDemoApplication.java @@ -1,12 +1,25 @@ 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; @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(); + + 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 new file mode 100644 index 00000000..d21186fb --- /dev/null +++ b/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java @@ -0,0 +1,22 @@ +package guru.springframework.controllers; + +import guru.springframework.services.GreetingService; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Controller; + +/** + * Created by jt on 5/24/17. + */ +@Controller +public class ConstructorInjectedController { + + private GreetingService greetingService; + + public ConstructorInjectedController(@Qualifier("constructorGreetingService") GreetingService greetingService) { + this.greetingService = greetingService; + } + + 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 new file mode 100644 index 00000000..39c3f3a7 --- /dev/null +++ b/src/main/java/guru/springframework/controllers/GetterInjectedController.java @@ -0,0 +1,23 @@ +package guru.springframework.controllers; + +import guru.springframework.services.GreetingService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Controller; + +/** + * Created by jt on 5/24/17. + */ +@Controller +public class GetterInjectedController { + private GreetingService greetingService; + + public String sayHello(){ + return greetingService.sayGreeting(); + } + + @Autowired + public void setGreetingService(@Qualifier("getterGreetingService") GreetingService greetingService) { + this.greetingService = greetingService; + } +} 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"; + } +} 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..33bf4dd5 --- /dev/null +++ b/src/main/java/guru/springframework/controllers/PropertyInjectedController.java @@ -0,0 +1,21 @@ +package guru.springframework.controllers; + +import guru.springframework.services.GreetingService; +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 GreetingService greetingServiceImpl; + + public String sayHello(){ + 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/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..e5e98f3d --- /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.greetingServiceImpl = new GreetingServiceImpl(); + } + + @Test + public void testGreeting() throws Exception { + assertEquals(GreetingServiceImpl.HELLO_GURUS, propertyInjectedController.sayHello()); + } +} \ No newline at end of file