BAEL-3290: Basic injection added
This commit is contained in:
parent
4c9417cff1
commit
3539c9900a
|
@ -0,0 +1,23 @@
|
||||||
|
package com.baeldung.dependency.ioc;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.baeldung.dependency.ioc;
|
||||||
|
|
||||||
|
public interface Item {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.baeldung.dependency.ioc;
|
||||||
|
|
||||||
|
public class ItemImpl1 implements Item {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.baeldung.dependency.ioc;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
<?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="ItemImpl1" 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" 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>
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.baeldung.dependency.ioc;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.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 XmlAppConfigTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("xml-store-by-constructor")
|
||||||
|
private Store storeByConstructorInjection;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidXmlConfig_WhenInjectStoreByConstructorInject_ThenBeanIsNotNull() {
|
||||||
|
assertNotNull(storeByConstructorInjection);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue