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/CustomBeanPostProcessor.java b/src/main/java/guru/springframework/CustomBeanPostProcessor.java new file mode 100644 index 00000000..5fd76bd2 --- /dev/null +++ b/src/main/java/guru/springframework/CustomBeanPostProcessor.java @@ -0,0 +1,30 @@ +package guru.springframework; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.stereotype.Component; + +/** + * Created by jt on 6/5/17. + */ +@Component +public class CustomBeanPostProcessor implements BeanPostProcessor { + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + + if(bean instanceof LifeCycleDemoBean){ + ((LifeCycleDemoBean) bean).beforeInit(); + } + + return bean; + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if(bean instanceof LifeCycleDemoBean){ + ((LifeCycleDemoBean) bean).afterInit(); + } + + return bean; + } +} diff --git a/src/main/java/guru/springframework/DiDemoApplication.java b/src/main/java/guru/springframework/DiDemoApplication.java index 9e42add3..5e1cc584 100644 --- a/src/main/java/guru/springframework/DiDemoApplication.java +++ b/src/main/java/guru/springframework/DiDemoApplication.java @@ -1,12 +1,24 @@ 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"); + + 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/LifeCycleDemoBean.java b/src/main/java/guru/springframework/LifeCycleDemoBean.java new file mode 100644 index 00000000..77d8645a --- /dev/null +++ b/src/main/java/guru/springframework/LifeCycleDemoBean.java @@ -0,0 +1,69 @@ +package guru.springframework; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.*; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; + +/** + * Created by jt on 6/5/17. + */ +@Component +public class LifeCycleDemoBean implements InitializingBean, DisposableBean, BeanNameAware, + BeanFactoryAware, ApplicationContextAware{ + + + public LifeCycleDemoBean() { + System.out.println("## I'm in the LifeCycleBean Constructor"); + } + + @Override + public void destroy() throws Exception { + System.out.println("## The Lifecycle bean has been terminated"); + + } + + @Override + public void afterPropertiesSet() throws Exception { + System.out.println("## The LifeCycleBean has its properties set!"); + + } + + @Override + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + System.out.println("## Bean Factory has been set"); + } + + @Override + public void setBeanName(String name) { + System.out.println("## My Bean Name is: " + name); + + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + System.out.println("## Application context has been set"); + } + + @PostConstruct + public void postConstruct(){ + System.out.println("## The Post Construct annotated method has been called"); + } + + @PreDestroy + public void preDestroy() { + System.out.println("## The Predestroy annotated method has been called"); + } + + public void beforeInit(){ + System.out.println("## - Before Init - Called by Bean Post Processor"); + } + + public void afterInit(){ + System.out.println("## - After init called by Bean Post Processor"); + } +} 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..683dbb99 --- /dev/null +++ b/src/main/java/guru/springframework/controllers/MyController.java @@ -0,0 +1,23 @@ +package guru.springframework.controllers; + +import guru.springframework.services.GreetingService; +import org.springframework.stereotype.Controller; + +/** + * Created by jt on 5/23/17. + */ +@Controller +public class MyController { + + private GreetingService greetingService; + + public MyController(GreetingService greetingService) { + this.greetingService = greetingService; + } + + public String hello(){ + System.out.println("Hello!!! "); + + return greetingService.sayGreeting(); + } +} 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..5ffddca4 --- /dev/null +++ b/src/main/java/guru/springframework/controllers/PropertyInjectedController.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 PropertyInjectedController { + + @Autowired + @Qualifier("greetingServiceImpl") + 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/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..5dddef65 --- /dev/null +++ b/src/main/java/guru/springframework/services/GreetingRepositoryImpl.java @@ -0,0 +1,27 @@ +package guru.springframework.services; + +import org.springframework.stereotype.Component; + +/** + * Created by jt on 5/24/17. + */ +@Component +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/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..4d169c20 --- /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!!!! - Original"; + + @Override + public String sayGreeting() { + return HELLO_GURUS; + } +} 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..738758a8 --- /dev/null +++ b/src/main/java/guru/springframework/services/PrimaryGermanGreetingService.java @@ -0,0 +1,25 @@ +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 { + + private GreetingRepository greetingRepository; + + public PrimaryGermanGreetingService(GreetingRepository greetingRepository) { + this.greetingRepository = greetingRepository; + } + + @Override + public String sayGreeting() { + return greetingRepository.getGermanGreeting(); + } +} 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..3289ba9d --- /dev/null +++ b/src/main/java/guru/springframework/services/PrimaryGreetingService.java @@ -0,0 +1,25 @@ +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({"en", "default"}) +public class PrimaryGreetingService implements GreetingService { + + private GreetingRepository greetingRepository; + + public PrimaryGreetingService(GreetingRepository greetingRepository) { + this.greetingRepository = greetingRepository; + } + + @Override + public String sayGreeting() { + return greetingRepository.getEnglishGreeting(); + } +} 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..14f32446 --- /dev/null +++ b/src/main/java/guru/springframework/services/PrimarySpanishGreetingService.java @@ -0,0 +1,25 @@ +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 { + + private GreetingRepository greetingRepository; + + public PrimarySpanishGreetingService(GreetingRepository greetingRepository) { + this.greetingRepository = greetingRepository; + } + + @Override + public String sayGreeting() { + return greetingRepository.getSpanishGreeting(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29b..cdc5de13 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +#spring.profiles.active=en \ No newline at end of file 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