Skip to content

Commit cdda6c2

Browse files
Spring constructor based DI
1 parent 9e5e748 commit cdda6c2

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package spring_constructor_based_di;
2+
3+
/**This one is also working fine.
4+
* But in order to make this one work, I'll have to enable
5+
* constructor-based DI in beans.xml and comment out other two DI methods in the TextEditor
6+
* and SpellChecker beans.*/
7+
import org.springframework.context.support.AbstractApplicationContext;
8+
import org.springframework.context.support.ClassPathXmlApplicationContext;
9+
10+
import common.TextEditor;
11+
12+
public class MainAppDemoConstructorBasedDI {
13+
public static void main(String[] args) {
14+
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
15+
"spring_constructor_based_di/beansForDemoConstructorBasedDI.xml");
16+
17+
TextEditor te = (TextEditor) context.getBean("textEditor");
18+
19+
te.spellCheck();
20+
context.registerShutdownHook();
21+
22+
System.out.println("Program ends.");
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<beans xmlns="http://www.springframework.org/schema/beans"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
7+
8+
<bean id="textEditor" class="common.TextEditor" lazy-init="true">
9+
<constructor-arg ref="spellChecker"/><!-- this is constructor based dependency injection -->
10+
</bean>
11+
<bean id="spellChecker" class="common.SpellChecker" lazy-init="true"></bean>
12+
13+
</beans>

0 commit comments

Comments
 (0)