From c174946f855ff445c787c8ddac683b73e26a0378 Mon Sep 17 00:00:00 2001 From: Alessio Stalla Date: Mon, 8 Jan 2018 22:12:47 +0100 Subject: [PATCH] Code for Alessio Stalla's evaluation article (Different Types of Bean Injection in Spring) --- .../baeldung/typesofbeaninjection/Config.java | 16 +++++ .../typesofbeaninjection/domain/Car.java | 24 ++++++++ .../typesofbeaninjection/domain/Engine.java | 24 ++++++++ .../domain/autowired/constructor/Car.java | 30 ++++++++++ .../domain/autowired/properties/Car.java | 30 ++++++++++ .../typesofbeaninjection-context.xml | 19 ++++++ .../TypeOfBeanInjectionUnitTest.java | 58 +++++++++++++++++++ 7 files changed, 201 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/typesofbeaninjection/Config.java create mode 100644 spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Car.java create mode 100644 spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Engine.java create mode 100644 spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/constructor/Car.java create mode 100644 spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/properties/Car.java create mode 100644 spring-core/src/main/resources/typesofbeaninjection-context.xml create mode 100644 spring-core/src/test/java/com/baeldung/typesofbeaninjection/TypeOfBeanInjectionUnitTest.java diff --git a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/Config.java b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/Config.java new file mode 100644 index 0000000000..a967a63005 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/Config.java @@ -0,0 +1,16 @@ +package com.baeldung.typesofbeaninjection; + +import com.baeldung.typesofbeaninjection.domain.Engine; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("com.baeldung.typesofbeaninjection") +public class Config { + + @Bean + public Engine engine() { + return new Engine("V8", 5); + } +} diff --git a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Car.java b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Car.java new file mode 100644 index 0000000000..878f369c8e --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Car.java @@ -0,0 +1,24 @@ +package com.baeldung.typesofbeaninjection.domain; + +public class Car { + private Engine engine; + + public Car() {} + + public Car(Engine engine) { + this.engine = engine; + } + + public Engine getEngine() { + return engine; + } + + public void setEngine(Engine engine) { + this.engine = engine; + } + + @Override + public String toString() { + return String.format("Car with %s engine", engine); + } +} diff --git a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Engine.java b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Engine.java new file mode 100644 index 0000000000..79e7a9856d --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Engine.java @@ -0,0 +1,24 @@ +package com.baeldung.typesofbeaninjection.domain; + +public class Engine { + private final String type; + private final int volume; + + public Engine(String type, int volume) { + this.type = type; + this.volume = volume; + } + + public String getType() { + return type; + } + + public int getVolume() { + return volume; + } + + @Override + public String toString() { + return String.format("%s %d", type, volume); + } +} diff --git a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/constructor/Car.java b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/constructor/Car.java new file mode 100644 index 0000000000..33fadf8d2b --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/constructor/Car.java @@ -0,0 +1,30 @@ +package com.baeldung.typesofbeaninjection.domain.autowired.constructor; + +import com.baeldung.typesofbeaninjection.domain.Engine; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component("car-autowired-by-constructor") +public class Car { + private Engine engine; + + public Car() {} + + @Autowired + public Car(Engine engine) { + this.engine = engine; + } + + public Engine getEngine() { + return engine; + } + + public void setEngine(Engine engine) { + this.engine = engine; + } + + @Override + public String toString() { + return String.format("Car with %s engine", engine); + } +} diff --git a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/properties/Car.java b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/properties/Car.java new file mode 100644 index 0000000000..1df33f5062 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/properties/Car.java @@ -0,0 +1,30 @@ +package com.baeldung.typesofbeaninjection.domain.autowired.properties; + +import com.baeldung.typesofbeaninjection.domain.Engine; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component("car-autowired-by-properties") +public class Car { + private Engine engine; + + public Car() {} + + public Car(Engine engine) { + this.engine = engine; + } + + public Engine getEngine() { + return engine; + } + + @Autowired + public void setEngine(Engine engine) { + this.engine = engine; + } + + @Override + public String toString() { + return String.format("Car with %s engine", engine); + } +} diff --git a/spring-core/src/main/resources/typesofbeaninjection-context.xml b/spring-core/src/main/resources/typesofbeaninjection-context.xml new file mode 100644 index 0000000000..5a9a0b59d7 --- /dev/null +++ b/spring-core/src/main/resources/typesofbeaninjection-context.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/typesofbeaninjection/TypeOfBeanInjectionUnitTest.java b/spring-core/src/test/java/com/baeldung/typesofbeaninjection/TypeOfBeanInjectionUnitTest.java new file mode 100644 index 0000000000..1b1319e001 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/typesofbeaninjection/TypeOfBeanInjectionUnitTest.java @@ -0,0 +1,58 @@ +package com.baeldung.typesofbeaninjection; + +import com.baeldung.typesofbeaninjection.domain.Car; +import com.baeldung.typesofbeaninjection.domain.Engine; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class TypeOfBeanInjectionUnitTest { + + @Test + public void whenConstructionInjectionInXML_thenCarHasEngine() { + ApplicationContext applicationContext + = new ClassPathXmlApplicationContext("/typesofbeaninjection-context.xml"); + Car car = applicationContext.getBean("alices-car", Car.class); + + checkEngine(car.getEngine()); + } + + @Test + public void whenPropertyInjectionInXML_thenCarHasEngine() { + ApplicationContext applicationContext + = new ClassPathXmlApplicationContext("/typesofbeaninjection-context.xml"); + Car car = applicationContext.getBean("bobs-car", Car.class); + + checkEngine(car.getEngine()); + } + + @Test + public void whenConstructionInjectionAnnotations_thenCarHasEngine() { + ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class); + com.baeldung.typesofbeaninjection.domain.autowired.constructor.Car car + = applicationContext.getBean(com.baeldung.typesofbeaninjection.domain.autowired.constructor.Car.class); + + checkEngine(car.getEngine()); + } + + @Test + public void whenPropertyInjectionAnnotations_thenCarHasEngine() { + ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class); + com.baeldung.typesofbeaninjection.domain.autowired.properties.Car car + = applicationContext.getBean(com.baeldung.typesofbeaninjection.domain.autowired.properties.Car.class); + + checkEngine(car.getEngine()); + } + + private void checkEngine(Engine engine) { + assertNotNull(engine); + assertEquals("V8", engine.getType()); + assertEquals(5, engine.getVolume()); + } + + +}