diff --git a/spring-di/src/main/java/org/baeldung/store/AppConfig.java b/spring-di/src/main/java/org/baeldung/store/AppConfig.java new file mode 100644 index 0000000000..80b6733dff --- /dev/null +++ b/spring-di/src/main/java/org/baeldung/store/AppConfig.java @@ -0,0 +1,23 @@ +package org.baeldung.store; + +import org.springframework.context.annotation.Bean; + +public class AppConfig { + + @Bean + public Item item1() { + return new ItemImpl1(); + } + + @Bean + public Store storeThroughConstructorInjection() { + return new Store(item1()); + } + + @Bean + public Store storeThroughSetterInjection() { + Store store = new Store(); + store.setItem(item1()); + return store; + } +} diff --git a/spring-di/src/main/java/org/baeldung/store/Item.java b/spring-di/src/main/java/org/baeldung/store/Item.java new file mode 100644 index 0000000000..1d7292fc35 --- /dev/null +++ b/spring-di/src/main/java/org/baeldung/store/Item.java @@ -0,0 +1,5 @@ +package org.baeldung.store; + +public interface Item { + +} diff --git a/spring-di/src/main/java/org/baeldung/store/ItemImpl1.java b/spring-di/src/main/java/org/baeldung/store/ItemImpl1.java new file mode 100644 index 0000000000..8bda9f24c9 --- /dev/null +++ b/spring-di/src/main/java/org/baeldung/store/ItemImpl1.java @@ -0,0 +1,5 @@ +package org.baeldung.store; + +public class ItemImpl1 implements Item { + +} diff --git a/spring-di/src/main/java/org/baeldung/store/Store.java b/spring-di/src/main/java/org/baeldung/store/Store.java new file mode 100644 index 0000000000..dcc2c3be48 --- /dev/null +++ b/spring-di/src/main/java/org/baeldung/store/Store.java @@ -0,0 +1,23 @@ +package org.baeldung.store; + +import org.springframework.beans.factory.annotation.Autowired; + +public class Store { + + @Autowired + private Item item; + + public Store() {} + + public Store(Item item) { + this.item = item; + } + + public Item getItem() { + return item; + } + + public void setItem(Item item) { + this.item = item; + } +} diff --git a/spring-di/src/main/resources/ioc-context-by-type.xml b/spring-di/src/main/resources/ioc-context-by-type.xml new file mode 100644 index 0000000000..1249ce51af --- /dev/null +++ b/spring-di/src/main/resources/ioc-context-by-type.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/spring-di/src/main/resources/ioc-context.xml b/spring-di/src/main/resources/ioc-context.xml new file mode 100644 index 0000000000..0e1d0ac29c --- /dev/null +++ b/spring-di/src/main/resources/ioc-context.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-di/src/test/java/org/baeldung/store/AppConfigUnitTest.java b/spring-di/src/test/java/org/baeldung/store/AppConfigUnitTest.java new file mode 100644 index 0000000000..3260114679 --- /dev/null +++ b/spring-di/src/test/java/org/baeldung/store/AppConfigUnitTest.java @@ -0,0 +1,35 @@ +package org.baeldung.store; + +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.beans.factory.annotation.Qualifier; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = AppConfig.class) +public class AppConfigUnitTest { + + @Autowired + @Qualifier("storeThroughConstructorInjection") + private Store storeByConstructorInjection; + + @Autowired + @Qualifier("storeThroughSetterInjection") + private Store storeBySetterInjection; + + @Test + public void givenValidXmlConfig_WhenInjectStoreByConstructorInjection_ThenBeanIsNotNull() { + assertNotNull(storeByConstructorInjection); + assertNotNull(storeByConstructorInjection.getItem()); + } + + @Test + public void givenValidXmlConfig_WhenInjectStoreBySetterInjection_ThenBeanIsNotNull() { + assertNotNull(storeBySetterInjection); + assertNotNull(storeByConstructorInjection.getItem()); + } +} diff --git a/spring-di/src/test/java/org/baeldung/store/XmlAppConfigByTypeUnitTest.java b/spring-di/src/test/java/org/baeldung/store/XmlAppConfigByTypeUnitTest.java new file mode 100644 index 0000000000..036399e537 --- /dev/null +++ b/spring-di/src/test/java/org/baeldung/store/XmlAppConfigByTypeUnitTest.java @@ -0,0 +1,30 @@ +package org.baeldung.store; + +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.beans.factory.annotation.Qualifier; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * Separate unit test class where only one Item object is available for + * autowiring. If the ioc-context.xml were used for autowiring by type, there + * would be multiple qualifying Item objects, causing a failure. + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:/ioc-context-by-type.xml") +public class XmlAppConfigByTypeUnitTest { + + @Autowired + @Qualifier("xml-store-by-autowire-type") + private Store storeByAutowireInjectionByType; + + @Test + public void givenValidXmlConfig_WhenInjectStoreByAutowireInjectionByType_ThenBeanIsNotNull() { + assertNotNull(storeByAutowireInjectionByType); + assertNotNull(storeByAutowireInjectionByType.getItem()); + } +} diff --git a/spring-di/src/test/java/org/baeldung/store/XmlAppConfigUnitTest.java b/spring-di/src/test/java/org/baeldung/store/XmlAppConfigUnitTest.java new file mode 100644 index 0000000000..2dd4d6ccd6 --- /dev/null +++ b/spring-di/src/test/java/org/baeldung/store/XmlAppConfigUnitTest.java @@ -0,0 +1,55 @@ +package org.baeldung.store; + +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.beans.factory.annotation.Qualifier; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:/ioc-context.xml") +public class XmlAppConfigUnitTest { + + @Autowired + @Qualifier("xml-store-by-constructor") + private Store storeByConstructorInjection; + + @Autowired + @Qualifier("xml-store-by-setter") + private Store storeBySetterInjection; + + @Autowired + @Qualifier("xml-store-by-autowire-name") + private Store storeByAutowireInjectionByName; + + @Autowired + @Qualifier("xml-store-by-setter-lazy") + private Store storeBySetterInjectionLazy; + + @Test + public void givenValidXmlConfig_WhenInjectStoreByConstructorInjection_ThenBeanIsNotNull() { + assertNotNull(storeByConstructorInjection); + assertNotNull(storeByConstructorInjection.getItem()); + } + + @Test + public void givenValidXmlConfig_WhenInjectStoreBySetterInjection_ThenBeanIsNotNull() { + assertNotNull(storeBySetterInjection); + assertNotNull(storeByConstructorInjection.getItem()); + } + + @Test + public void givenValidXmlConfig_WhenInjectStoreByAutowireInjectionByName_ThenBeanIsNotNull() { + assertNotNull(storeByAutowireInjectionByName); + assertNotNull(storeByAutowireInjectionByName.getItem()); + } + + @Test + public void givenValidXmlConfig_WhenInjectStoreBySetterInjectionLazy_ThenBeanIsNotNull() { + assertNotNull(storeBySetterInjectionLazy); + assertNotNull(storeByConstructorInjection.getItem()); + } +}