Skip to content

Commit 0eba8e7

Browse files
committed
add xml bean sample
1 parent 08f8259 commit 0eba8e7

File tree

9 files changed

+238
-0
lines changed

9 files changed

+238
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### XML方式配置Bean实战
2+
3+
4+
5+
#### 配置方式
6+
7+
- 无参构造
8+
- 有参构造
9+
- 静态工厂方法
10+
- 实例工厂方法
11+
12+
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example.springboot.source.code.analysis.xml;
2+
3+
/**
4+
* Created by ipipman on 2021/9/22.
5+
*
6+
* @version V1.0
7+
* @Package com.example.springboot.source.code.analysis.xml
8+
* @Description: (用一句话描述该文件做什么)
9+
* @date 2021/9/22 10:34 上午
10+
*/
11+
public abstract class Animal {
12+
13+
abstract String getName();
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.springboot.source.code.analysis.xml;
2+
3+
/**
4+
* Created by ipipman on 2021/9/22.
5+
*
6+
* @version V1.0
7+
* @Package com.example.springboot.source.code.analysis.xml
8+
* @Description: (用一句话描述该文件做什么)
9+
* @date 2021/9/22 10:44 上午
10+
*/
11+
public class AnimalFactory {
12+
13+
// 静态工厂
14+
public static Animal getAnimal(String type) {
15+
if ("Dog".equals(type)) {
16+
return new Dog();
17+
} else {
18+
return new Cat();
19+
}
20+
}
21+
22+
// 实例工厂
23+
public Animal getAnimal1(String type) {
24+
if ("Dog".equals(type)) {
25+
return new Dog();
26+
} else {
27+
return new Cat();
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.example.springboot.source.code.analysis.xml;
2+
3+
/**
4+
* Created by ipipman on 2021/9/22.
5+
*
6+
* @version V1.0
7+
* @Package com.example.springboot.source.code.analysis.xml
8+
* @Description: (用一句话描述该文件做什么)
9+
* @date 2021/9/22 10:43 上午
10+
*/
11+
public class Cat extends Animal{
12+
@Override
13+
String getName() {
14+
return "Cat";
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.example.springboot.source.code.analysis.xml;
2+
3+
/**
4+
* Created by ipipman on 2021/9/22.
5+
*
6+
* @version V1.0
7+
* @Package com.example.springboot.source.code.analysis.xml
8+
* @Description: (用一句话描述该文件做什么)
9+
* @date 2021/9/22 10:43 上午
10+
*/
11+
public class Dog extends Animal{
12+
@Override
13+
String getName() {
14+
return "Dog";
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.example.springboot.source.code.analysis.xml;
2+
3+
/**
4+
* Created by ipipman on 2021/9/22.
5+
*
6+
* @version V1.0
7+
* @Package com.example.springboot.source.code.analysis.xml
8+
* @Description: (用一句话描述该文件做什么)
9+
* @date 2021/9/22 10:26 上午
10+
*/
11+
public class HelloService {
12+
13+
private Student student;
14+
15+
private Animal animal;
16+
17+
public Animal getAnimal() {
18+
return animal;
19+
}
20+
21+
public void setAnimal(Animal animal) {
22+
this.animal = animal;
23+
}
24+
25+
public Student getStudent() {
26+
return student;
27+
}
28+
29+
public void setStudent(Student student) {
30+
this.student = student;
31+
}
32+
33+
public String hello() {
34+
//return student.toString();
35+
return animal.getName();
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.example.springboot.source.code.analysis.xml;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Created by ipipman on 2021/9/22.
7+
*
8+
* @version V1.0
9+
* @Package com.example.springboot.source.code.analysis.xml
10+
* @Description: (用一句话描述该文件做什么)
11+
* @date 2021/9/22 10:19 上午
12+
*/
13+
public class Student {
14+
15+
private String name;
16+
private Integer age;
17+
private List<String> classList;
18+
19+
/**
20+
* 使用构造器注入
21+
*
22+
* @param name
23+
* @param age
24+
*/
25+
public Student(String name, Integer age) {
26+
this.name = name;
27+
this.age = age;
28+
}
29+
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public Integer getAge() {
40+
return age;
41+
}
42+
43+
public void setAge(Integer age) {
44+
this.age = age;
45+
}
46+
47+
public List<String> getClassList() {
48+
return classList;
49+
}
50+
51+
public void setClassList(List<String> classList) {
52+
this.classList = classList;
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return "Student{" +
58+
"name='" + name + '\'' +
59+
", age=" + age +
60+
", classList=" + String.join(",", classList) +
61+
'}';
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5+
6+
<bean id="student" class="com.example.springboot.source.code.analysis.xml.Student">
7+
<!-- 有参构造器 -->
8+
<constructor-arg index="0" value="zhangsan"/>
9+
<constructor-arg index="1" value="13"/>
10+
<property name="name" value="zhangsan"/>
11+
<property name="age" value="13"/>
12+
<property name="classList">
13+
<list>
14+
<value>math</value>
15+
<value>english</value>
16+
</list>
17+
</property>
18+
</bean>
19+
20+
<bean id="helloService" class="com.example.springboot.source.code.analysis.xml.HelloService">
21+
<property name="student" ref="student"/>
22+
<property name="animal" ref="cat"/>
23+
</bean>
24+
25+
<!-- 静态工厂类 -->
26+
<bean id="dog" class="com.example.springboot.source.code.analysis.xml.AnimalFactory" factory-method="getAnimal">
27+
<constructor-arg value="Dog"/>
28+
</bean>
29+
30+
<!-- 实例工厂类 -->
31+
<bean name="animalFactory" class="com.example.springboot.source.code.analysis.xml.AnimalFactory"/>
32+
<bean id="cat" factory-bean="animalFactory" factory-method="getAnimal1">
33+
<constructor-arg value="Cat"/>
34+
</bean>
35+
36+
</beans>

springboot-source-code-analysis/src/test/java/com/example/springboot/source/code/analysis/SpringbootSourceCodeAnalysisApplicationTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import com.example.springboot.source.code.analysis.event.RainListener;
44
import com.example.springboot.source.code.analysis.event.WeatherRunListener;
55
import com.example.springboot.source.code.analysis.listener.ApplicationContextContainer;
6+
import com.example.springboot.source.code.analysis.xml.HelloService;
67
import org.junit.Test;
78
import org.junit.runner.RunWith;
89
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.context.ContextConfiguration;
1012
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1113

1214
@SpringBootTest(classes = SpringbootSourceCodeAnalysisApplication.class)
1315
@RunWith(SpringJUnit4ClassRunner.class)
16+
@ContextConfiguration(locations = "classpath:ioc/demo.xml")
1417
public class SpringbootSourceCodeAnalysisApplicationTests {
1518

1619
// 引入事件监听器
@@ -33,6 +36,15 @@ public void testContextRefreshEvent() {
3336
weatherRunListener.rain();
3437
}
3538

39+
// 引入XML Bean
40+
@Autowired
41+
private HelloService helloService;
42+
43+
@Test
44+
public void testHello(){
45+
System.out.println(helloService.hello());
46+
}
47+
3648

3749
@Test
3850
public void contextLoads() {

0 commit comments

Comments
 (0)