From ae47827879d0e9396ad5de5f979f495629a35838 Mon Sep 17 00:00:00 2001 From: alv21 Date: Thu, 8 Feb 2018 19:15:56 +0100 Subject: [PATCH] Different Types of Bean Injection in Spring --- .../beansinjectiontypes/Cabriolet.java | 8 +++ .../com/baeldung/beansinjectiontypes/Car.java | 6 ++ .../DealershipTraditionalStyle.java | 9 +++ .../DealershipWithDependenciesInjection.java | 13 +++++ .../baeldung/beansinjectiontypes/Sedan.java | 10 ++++ .../constructor/Config.java | 20 +++++++ .../constructor/Dealership.java | 20 +++++++ .../constructor/HornCheckerFile.java | 15 +++++ .../constructor/HornCheckerXML.java | 15 +++++ .../beansinjectiontypes/setter/Config.java | 22 ++++++++ .../setter/Dealership.java | 19 +++++++ .../setter/HornCheckerFile.java | 15 +++++ .../setter/HornCheckerXML.java | 15 +++++ .../resources/beansinjectiontypes-ctx.xml | 10 ++++ .../BeansInjectionTypesIntegrationTest.java | 56 +++++++++++++++++++ 15 files changed, 253 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/beansinjectiontypes/Cabriolet.java create mode 100644 spring-core/src/main/java/com/baeldung/beansinjectiontypes/Car.java create mode 100644 spring-core/src/main/java/com/baeldung/beansinjectiontypes/DealershipTraditionalStyle.java create mode 100644 spring-core/src/main/java/com/baeldung/beansinjectiontypes/DealershipWithDependenciesInjection.java create mode 100644 spring-core/src/main/java/com/baeldung/beansinjectiontypes/Sedan.java create mode 100644 spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/Config.java create mode 100644 spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/Dealership.java create mode 100644 spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/HornCheckerFile.java create mode 100644 spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/HornCheckerXML.java create mode 100644 spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/Config.java create mode 100644 spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/Dealership.java create mode 100644 spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/HornCheckerFile.java create mode 100644 spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/HornCheckerXML.java create mode 100644 spring-core/src/main/resources/beansinjectiontypes-ctx.xml create mode 100644 spring-core/src/test/java/com/baeldung/beansinjectiontypes/BeansInjectionTypesIntegrationTest.java diff --git a/spring-core/src/main/java/com/baeldung/beansinjectiontypes/Cabriolet.java b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/Cabriolet.java new file mode 100644 index 0000000000..2bf9b8b954 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/Cabriolet.java @@ -0,0 +1,8 @@ +package com.baeldung.beansinjectiontypes; + +public class Cabriolet implements Car{ + + public void honk(){ + System.out.println("I'm a cabriolet!"); + } +} diff --git a/spring-core/src/main/java/com/baeldung/beansinjectiontypes/Car.java b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/Car.java new file mode 100644 index 0000000000..e88e218f95 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/Car.java @@ -0,0 +1,6 @@ +package com.baeldung.beansinjectiontypes; + +public interface Car { + + public void honk(); +} diff --git a/spring-core/src/main/java/com/baeldung/beansinjectiontypes/DealershipTraditionalStyle.java b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/DealershipTraditionalStyle.java new file mode 100644 index 0000000000..b917049e41 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/DealershipTraditionalStyle.java @@ -0,0 +1,9 @@ +package com.baeldung.beansinjectiontypes; + +public class DealershipTraditionalStyle { + private Car car = new Sedan(); + + public Car getCar() { + return car; + } +} diff --git a/spring-core/src/main/java/com/baeldung/beansinjectiontypes/DealershipWithDependenciesInjection.java b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/DealershipWithDependenciesInjection.java new file mode 100644 index 0000000000..9b25e21dc7 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/DealershipWithDependenciesInjection.java @@ -0,0 +1,13 @@ +package com.baeldung.beansinjectiontypes; + +public class DealershipWithDependenciesInjection { + private Car car; + + public DealershipWithDependenciesInjection(Car car) { + this.car = car; + } + + public Car getCar() { + return car; + } +} diff --git a/spring-core/src/main/java/com/baeldung/beansinjectiontypes/Sedan.java b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/Sedan.java new file mode 100644 index 0000000000..0ae4cf5a40 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/Sedan.java @@ -0,0 +1,10 @@ +package com.baeldung.beansinjectiontypes; + +import org.springframework.stereotype.Component; + +@Component +public class Sedan implements Car { + public void honk(){ + System.out.println("I'm a sedan!"); + } +} diff --git a/spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/Config.java b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/Config.java new file mode 100644 index 0000000000..3e2534f892 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/Config.java @@ -0,0 +1,20 @@ +package com.baeldung.beansinjectiontypes.constructor; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.beansinjectiontypes.Car; +import com.baeldung.beansinjectiontypes.Sedan; + +@Configuration(value="constructorConfig") +public class Config { + @Bean + public Car car() { + return new Sedan(); + } + + @Bean + public Dealership dealership() { + return new Dealership(car()); + } +} diff --git a/spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/Dealership.java b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/Dealership.java new file mode 100644 index 0000000000..6151ec8e70 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/Dealership.java @@ -0,0 +1,20 @@ +package com.baeldung.beansinjectiontypes.constructor; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.baeldung.beansinjectiontypes.Car; + +@Component(value="constructorDealership") +public class Dealership { + private Car car; + + @Autowired + public Dealership(Car car) { + this.car = car; + } + + public Car getCar() { + return car; + } +} diff --git a/spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/HornCheckerFile.java b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/HornCheckerFile.java new file mode 100644 index 0000000000..924372e3df --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/HornCheckerFile.java @@ -0,0 +1,15 @@ +package com.baeldung.beansinjectiontypes.constructor; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class HornCheckerFile { + + public static void main(String[] args) { + try (AnnotationConfigApplicationContext context = + new AnnotationConfigApplicationContext(Config.class)) { + Dealership dealership = (Dealership) context.getBean("dealership"); + + dealership.getCar().honk(); + } + } +} diff --git a/spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/HornCheckerXML.java b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/HornCheckerXML.java new file mode 100644 index 0000000000..3c035986e6 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/HornCheckerXML.java @@ -0,0 +1,15 @@ +package com.baeldung.beansinjectiontypes.constructor; + +import org.springframework.context.support.FileSystemXmlApplicationContext; + +public class HornCheckerXML { + + public static void main(String[] args) { + try (FileSystemXmlApplicationContext context = + new FileSystemXmlApplicationContext("src/main/resources/beansinjectiontypes-ctx.xml")) { + Dealership dealership = (Dealership) context.getBean("constructorDealership"); + + dealership.getCar().honk(); + } + } +} diff --git a/spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/Config.java b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/Config.java new file mode 100644 index 0000000000..212d303ce7 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/Config.java @@ -0,0 +1,22 @@ +package com.baeldung.beansinjectiontypes.setter; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.beansinjectiontypes.Car; +import com.baeldung.beansinjectiontypes.Sedan; + +@Configuration(value="setterConfig") +public class Config { + @Bean + public Car car() { + return new Sedan(); + } + + @Bean + public Dealership dealership() { + Dealership dealership = new Dealership(); + dealership.setCar(car()); + return dealership; + } +} diff --git a/spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/Dealership.java b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/Dealership.java new file mode 100644 index 0000000000..db9c7cbceb --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/Dealership.java @@ -0,0 +1,19 @@ +package com.baeldung.beansinjectiontypes.setter; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.baeldung.beansinjectiontypes.Car; + +@Component(value="setterDealership") +public class Dealership { + private Car car; + + @Autowired + public void setCar(Car car) { + this.car = car; + } + public Car getCar() { + return car; + } +} diff --git a/spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/HornCheckerFile.java b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/HornCheckerFile.java new file mode 100644 index 0000000000..3372635bd3 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/HornCheckerFile.java @@ -0,0 +1,15 @@ +package com.baeldung.beansinjectiontypes.setter; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class HornCheckerFile { + + public static void main(String[] args) { + try (AnnotationConfigApplicationContext context = + new AnnotationConfigApplicationContext(Config.class)) { + Dealership dealership = (Dealership) context.getBean("dealership"); + + dealership.getCar().honk(); + } + } +} diff --git a/spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/HornCheckerXML.java b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/HornCheckerXML.java new file mode 100644 index 0000000000..0863d16d86 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/HornCheckerXML.java @@ -0,0 +1,15 @@ +package com.baeldung.beansinjectiontypes.setter; + +import org.springframework.context.support.FileSystemXmlApplicationContext; + +public class HornCheckerXML { + + public static void main(String[] args) { + try (FileSystemXmlApplicationContext context = + new FileSystemXmlApplicationContext("src/main/resources/beansinjectiontypes-ctx.xml")) { + Dealership dealership = (Dealership) context.getBean("setterDealership"); + + dealership.getCar().honk(); + } + } +} diff --git a/spring-core/src/main/resources/beansinjectiontypes-ctx.xml b/spring-core/src/main/resources/beansinjectiontypes-ctx.xml new file mode 100644 index 0000000000..86131a83b1 --- /dev/null +++ b/spring-core/src/main/resources/beansinjectiontypes-ctx.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/beansinjectiontypes/BeansInjectionTypesIntegrationTest.java b/spring-core/src/test/java/com/baeldung/beansinjectiontypes/BeansInjectionTypesIntegrationTest.java new file mode 100644 index 0000000000..9ffa44ced7 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/beansinjectiontypes/BeansInjectionTypesIntegrationTest.java @@ -0,0 +1,56 @@ +package com.baeldung.beansinjectiontypes; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.FileSystemXmlApplicationContext; + +import com.baeldung.beansinjectiontypes.constructor.Config; +import com.baeldung.beansinjectiontypes.constructor.Dealership; + +public class BeansInjectionTypesIntegrationTest { + + @Test + public void configBean_WhenSetOnConstructor_ThenDependencyValid() { + try (AnnotationConfigApplicationContext context = + new AnnotationConfigApplicationContext(Config.class)) { + Dealership dealership = (Dealership) context.getBean("dealership"); + + assertTrue(dealership.getCar() instanceof Sedan); + } + } + + @Test + public void configBean_WhenSetOnSetter_ThenDependencyValid() { + try (AnnotationConfigApplicationContext context = + new AnnotationConfigApplicationContext(com.baeldung.beansinjectiontypes.setter.Config.class)) { + com.baeldung.beansinjectiontypes.setter.Dealership dealership = + (com.baeldung.beansinjectiontypes.setter.Dealership) context.getBean("dealership"); + + assertTrue(dealership.getCar() instanceof Sedan); + } + } + + @Test + public void annotationAndXML_WhenSetOnSetter_ThenDependencyValid() { + try (FileSystemXmlApplicationContext context = + new FileSystemXmlApplicationContext("src/main/resources/beansinjectiontypes-ctx.xml")) { + Dealership dealership = (Dealership) context.getBean("constructorDealership"); + + assertTrue(dealership.getCar() instanceof Sedan); + } + } + + @Test + public void annotationAndXML_WhenSetOnConstructor_ThenDependencyValid() { + try (FileSystemXmlApplicationContext context = + new FileSystemXmlApplicationContext("src/main/resources/beansinjectiontypes-ctx.xml")) { + com.baeldung.beansinjectiontypes.setter.Dealership dealership = + (com.baeldung.beansinjectiontypes.setter.Dealership) context.getBean("setterDealership"); + + assertTrue(dealership.getCar() instanceof Sedan); + } + } + +}