Skip to content

Commit bfbbabb

Browse files
committed
BeanFactory和ApplicationContext的对比
1 parent e91b4b2 commit bfbbabb

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

SpringLearn_No_Web/src/edu/xpu/service/UserServiceImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ public void setName(String name) {
1616
public void add() {
1717
System.out.println("添加用户..."+name);
1818
}
19+
20+
public UserServiceImpl() {
21+
22+
System.out.println("调用UserServiceImpl");
23+
24+
}
1925
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
package edu.xpu.test;
3+
4+
import edu.xpu.service.IUserService;
5+
import org.springframework.context.ApplicationContext;
6+
import org.springframework.context.support.ClassPathXmlApplicationContext;
7+
8+
9+
public class Lesson01 {
10+
11+
public static void test1(){
12+
13+
14+
// Spring 容器加载的三种方式
15+
// 第一种 ClassPathXmlApplicationContext ClassPath:指的就是classes路径 最常用的
16+
17+
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
18+
19+
// 第二种 文件系统路径获得配置文件[绝对路径获取]
20+
21+
// ApplicationContext context = new FileSystemXmlApplicationContext("/Users/rex/Desktop/Code/Java/JavaCode/SpringLearn_No_Web/src/edu/xpu/beans.xml");
22+
23+
// 第三种 使用BeanFactory 了解即可已经过时
24+
25+
// String path = "/Users/rex/Desktop/Code/Java/JavaCode/SpringLearn_No_Web/src/edu/xpu/beans.xml";
26+
27+
//BeanFactory factory = new XmlBeanFactory(new FileSystemResource(path));
28+
29+
//IUserService user = (IUserService)factory.getBean("userService");
30+
IUserService user = (IUserService)context.getBean("userService");
31+
user.add();
32+
33+
}
34+
35+
public static void main(String[] args) {
36+
37+
Lesson01.test1();
38+
}
39+
40+
}
41+

SpringLearn_No_Web/src/edu/xpu/test/Lesson02.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,42 @@
22
package edu.xpu.test;
33

44
import edu.xpu.service.IUserService;
5+
import org.springframework.beans.factory.BeanFactory;
6+
import org.springframework.beans.factory.xml.XmlBeanFactory;
57
import org.springframework.context.ApplicationContext;
68
import org.springframework.context.support.ClassPathXmlApplicationContext;
7-
9+
import org.springframework.core.io.FileSystemResource;
810

911
public class Lesson02 {
1012

1113
public static void test1(){
1214

13-
14-
// Spring 容器加载的三种方式
15-
// 第一种 ClassPathXmlApplicationContext ClassPath:指的就是classes路径 最常用的
15+
/*
16+
*
17+
* BeanFactory 采用延迟加载,第一次getBean时才会初始化Bean
18+
* ApplicationContext 是对BeanFactory的拓展,提供的更多的功能
19+
* 国际化处理
20+
* 事件传递
21+
* Bean自动装配
22+
* 各种不同应用层的Context实现
23+
*
24+
* */
1625

1726
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
1827

19-
// 第二种 文件系统路径获得配置文件[绝对路径获取]
20-
21-
// ApplicationContext context = new FileSystemXmlApplicationContext("/Users/rex/Desktop/Code/Java/JavaCode/SpringLearn_No_Web/src/edu/xpu/beans.xml");
22-
23-
// 第三种 使用BeanFactory 了解即可已经过时
24-
25-
// String path = "/Users/rex/Desktop/Code/Java/JavaCode/SpringLearn_No_Web/src/edu/xpu/beans.xml";
28+
//String path = "/Users/rex/Desktop/Code/Java/JavaCode/SpringLearn_No_Web/src/beans.xml";
2629

2730
//BeanFactory factory = new XmlBeanFactory(new FileSystemResource(path));
2831

29-
//IUserService user = (IUserService)factory.getBean("userService");
3032
IUserService user = (IUserService)context.getBean("userService");
33+
//IUserService user = (IUserService)factory.getBean("userService");
3134
user.add();
3235

3336
}
3437

3538
public static void main(String[] args) {
3639

3740
Lesson02.test1();
38-
}
3941

42+
}
4043
}
41-

0 commit comments

Comments
 (0)