Merge pull request #8346 from albanoj2/BAEL-3290

BAEL-3290: Added source code for Spring DI examples
This commit is contained in:
Eric Martin 2019-12-11 19:53:14 -06:00 committed by GitHub
commit 92b1b5270c
9 changed files with 224 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -0,0 +1,5 @@
package org.baeldung.store;
public interface Item {
}

View File

@ -0,0 +1,5 @@
package org.baeldung.store;
public class ItemImpl1 implements Item {
}

View File

@ -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;
}
}

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Autowired injection -->
<bean id="item" class="org.baeldung.store.ItemImpl1" />
<bean id="xml-store-by-autowire-type" class="org.baeldung.store.Store" autowire="byType">
</bean>
</beans>

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Constructor injection -->
<bean id="item1" class="org.baeldung.store.ItemImpl1" />
<bean id="xml-store-by-constructor" class="org.baeldung.store.Store">
<constructor-arg type="Item" index="0" name="item" ref="item1" />
</bean>
<!-- Setter injection -->
<bean id="xml-store-by-setter" class="org.baeldung.store.Store">
<property name="item" ref="item1" />
</bean>
<!-- Autowired injection -->
<bean id="item" class="org.baeldung.store.ItemImpl1" />
<bean id="xml-store-by-autowire-name" class="org.baeldung.store.Store" autowire="byName">
</bean>
<!-- Lazy instantiation -->
<bean id="item1-lazy" class="org.baeldung.store.ItemImpl1" lazy-init="true" />
<bean id="xml-store-by-setter-lazy" class="org.baeldung.store.Store">
<property name="item" ref="item1-lazy" />
</bean>
</beans>

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}