Skip to content

Commit 247cf18

Browse files
pure XML DI using Spring
1 parent 8e92cd2 commit 247cf18

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package pureXMLDependencyInjecttionExample;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
6+
7+
@ToString
8+
public class Career {
9+
@Getter
10+
@Setter
11+
private String jobType;
12+
13+
@Getter
14+
@Setter
15+
private int jobLevel;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package pureXMLDependencyInjecttionExample;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
6+
7+
@ToString
8+
public class City {
9+
@Getter
10+
@Setter
11+
private String cityName;
12+
13+
@Getter
14+
@Setter
15+
private String area;
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package pureXMLDependencyInjecttionExample;
2+
3+
import org.springframework.context.support.AbstractApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
6+
/**This app is running fine!
7+
* Remember to add apache.commson.logging and spring jars onto your classpath, otherwise the program won't run.
8+
* It helps me understand how @Resource annotation works! Cool!
9+
* Look at this project along with other two:
10+
* SpringPureXMLDependencyInjectionExample
11+
* Spring@RessourceAnnotationExample
12+
* to help me better understand the three possible ways to do DI using Spring.*/
13+
public class MainApp {
14+
15+
public static void main(String... args) {
16+
System.out.println("It started!");
17+
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
18+
Person p =(Person) context.getBean("person");
19+
20+
System.out.println(p.getCity().toString());
21+
System.out.println(p.getWife().toString());
22+
System.out.println(p.getCareer().toString());
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package pureXMLDependencyInjecttionExample;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
public class Person {
7+
@Getter
8+
@Setter
9+
private City city;
10+
11+
@Getter
12+
@Setter
13+
private Wife wife;
14+
15+
@Getter
16+
@Setter
17+
private Career career;
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package pureXMLDependencyInjecttionExample;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
6+
7+
@ToString
8+
public class Wife {
9+
10+
@Getter
11+
@Setter
12+
private String name;
13+
14+
@Getter
15+
@Setter
16+
private String faith;
17+
18+
}

SpringTutorials/src/spring.xml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7+
http://www.springframework.org/schema/context
8+
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
9+
10+
<bean id="person" class="pureXMLDependencyInjecttionExample.Person">
11+
<property name="city" ref="home123"/>
12+
<property name="wife" ref="wife"/>
13+
<property name="career" ref="careerFuture"/>
14+
</bean>
15+
16+
<bean id="home123" class="pureXMLDependencyInjecttionExample.City">
17+
<property name="cityName" value="San Jose"/>
18+
<property name="area" value="NorCal"/>
19+
</bean>
20+
21+
<bean id="wife" class="pureXMLDependencyInjecttionExample.Wife">
22+
<property name="name" value="Sophie Yan"/>
23+
<property name="faith" value="Christian"/>
24+
</bean>
25+
26+
<bean id = "careerCurrent" class="pureXMLDependencyInjecttionExample.Career">
27+
<property name="jobType" value="SDE"/>
28+
<property name="jobLevel" value="4"/>
29+
</bean>
30+
<bean id="careerFuture" class="pureXMLDependencyInjecttionExample.Career">
31+
<property name="jobType" value="SDE"/>
32+
<property name="jobLevel" value="5"/>
33+
</bean>
34+
35+
</beans>

0 commit comments

Comments
 (0)