diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Boat.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Boat.java new file mode 100644 index 0000000000..a7138aff40 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Boat.java @@ -0,0 +1,19 @@ +package com.baeldung.beaninjectiontypes; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class Boat { + + private Engine engine; + + @Autowired + public void setEngine(Engine engine) { + this.engine = engine; + } + + public Engine getEngine() { + return engine; + } +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Car.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Car.java new file mode 100644 index 0000000000..d943a0a839 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Car.java @@ -0,0 +1,18 @@ +package com.baeldung.beaninjectiontypes; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class Car { + @Autowired + private Engine engine; + + public void setEngine(Engine engine) { + this.engine = engine; + } + + public Engine getEngine() { + return engine; + } +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Config.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Config.java new file mode 100644 index 0000000000..928ea1d794 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Config.java @@ -0,0 +1,10 @@ +package com.baeldung.beaninjectiontypes; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("com.baeldung.beaninjectiontypes") +public class Config { + +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Engine.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Engine.java new file mode 100644 index 0000000000..71c9a5855b --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Engine.java @@ -0,0 +1,8 @@ +package com.baeldung.beaninjectiontypes; + +import org.springframework.stereotype.Component; + +@Component +public class Engine { + +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/LawnMower.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/LawnMower.java new file mode 100644 index 0000000000..19b71bf8d3 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/LawnMower.java @@ -0,0 +1,19 @@ +package com.baeldung.beaninjectiontypes; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class LawnMower { + + @Autowired + private Engine engine; + + public LawnMower(Engine engine) { + this.engine = engine; + } + + public Engine getEngine() { + return engine; + } +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Rocket.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Rocket.java new file mode 100644 index 0000000000..7bfbc6fc01 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Rocket.java @@ -0,0 +1,20 @@ +package com.baeldung.beaninjectiontypes; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class Rocket { + + private Engine engine; + + @Autowired + public Rocket(Engine engine) { + this.engine = engine; + } + + public Engine getEngine() { + return engine; + } + +} diff --git a/spring-core/src/test/java/com/baeldung/beaninjectiontypes/BoatTest.java b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/BoatTest.java new file mode 100644 index 0000000000..f89a14e942 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/BoatTest.java @@ -0,0 +1,23 @@ +package com.baeldung.beaninjectiontypes; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = Config.class) +public class BoatTest { + + @Autowired + Boat boat; + + @Test + public void engineInjectionTest() { + assertNotNull(boat); + assertNotNull(boat.getEngine()); + } +} diff --git a/spring-core/src/test/java/com/baeldung/beaninjectiontypes/CarTest.java b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/CarTest.java new file mode 100644 index 0000000000..7c022f891b --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/CarTest.java @@ -0,0 +1,23 @@ +package com.baeldung.beaninjectiontypes; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = Config.class) +public class CarTest { + + @Autowired + Car car; + + @Test + public void engineInjectionTest() { + assertNotNull(car); + assertNotNull(car.getEngine()); + } +} diff --git a/spring-core/src/test/java/com/baeldung/beaninjectiontypes/LawnMowerTest.java b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/LawnMowerTest.java new file mode 100644 index 0000000000..84595c717d --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/LawnMowerTest.java @@ -0,0 +1,23 @@ +package com.baeldung.beaninjectiontypes; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = Config.class) +public class LawnMowerTest { + + @Autowired + LawnMower lawnMower; + + @Test + public void engineInjectionTest() { + assertNotNull(lawnMower); + assertNotNull(lawnMower.getEngine()); + } +} diff --git a/spring-core/src/test/java/com/baeldung/beaninjectiontypes/RocketTest.java b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/RocketTest.java new file mode 100644 index 0000000000..3fe44d266e --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/RocketTest.java @@ -0,0 +1,23 @@ +package com.baeldung.beaninjectiontypes; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = Config.class) +public class RocketTest { + + @Autowired + Rocket rocket; + + @Test + public void engineInjectionTest() { + assertNotNull(rocket); + assertNotNull(rocket.getEngine()); + } +}