Skip to content

Commit cd42a8c

Browse files
committed
ClassPathXmlApplicationContext构造器的前两个方法调用部分注释
1 parent 3dc4f43 commit cd42a8c

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
215215
@Nullable
216216
private Thread shutdownHook;
217217

218+
/*
219+
* 在 new 容器的时候,会将该资源初始化
220+
*/
218221
/** ResourcePatternResolver used by this context. */
219222
private ResourcePatternResolver resourcePatternResolver;
220223

@@ -249,7 +252,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
249252
* Create a new AbstractApplicationContext with no parent.
250253
*/
251254
public AbstractApplicationContext() {
252-
this.resourcePatternResolver = getResourcePatternResolver();
255+
this.resourcePatternResolver = getResourcePatternResolver(); // 获取资源解析器
253256
}
254257

255258
/**
@@ -258,7 +261,7 @@ public AbstractApplicationContext() {
258261
*/
259262
public AbstractApplicationContext(@Nullable ApplicationContext parent) {
260263
this();
261-
setParent(parent);
264+
setParent(parent); // parent 默认为 null, 设置当前的 parent 参数为 null
262265
}
263266

264267

@@ -489,7 +492,7 @@ LifecycleProcessor getLifecycleProcessor() throws IllegalStateException {
489492
* @see org.springframework.core.io.support.PathMatchingResourcePatternResolver
490493
*/
491494
protected ResourcePatternResolver getResourcePatternResolver() {
492-
return new PathMatchingResourcePatternResolver(this);
495+
return new PathMatchingResourcePatternResolver(this); // 该解析器支持 Ant 风格,简单的 url 协议。例如:file:// 这种也可以解析。 同时传入 resourceLoader
493496
}
494497

495498

spring-context/src/main/java/org/springframework/context/support/AbstractRefreshableConfigApplicationContext.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public void setConfigLocation(String location) {
7070
}
7171

7272
/**
73+
* @param locations 传入参数类型: classpath:/applicationContext.xml
7374
* Set the config locations for this application context.
7475
* <p>If not set, the implementation may use a default as appropriate.
7576
*/
@@ -78,7 +79,7 @@ public void setConfigLocations(@Nullable String... locations) {
7879
Assert.noNullElements(locations, "Config locations must not be null");
7980
this.configLocations = new String[locations.length];
8081
for (int i = 0; i < locations.length; i++) {
81-
this.configLocations[i] = resolvePath(locations[i]).trim();
82+
this.configLocations[i] = resolvePath(locations[i]).trim(); // 如果传入的是多个路径,向数组中添加值。
8283
}
8384
}
8485
else {
@@ -122,7 +123,7 @@ protected String[] getDefaultConfigLocations() {
122123
* @see org.springframework.core.env.Environment#resolveRequiredPlaceholders(String)
123124
*/
124125
protected String resolvePath(String path) {
125-
return getEnvironment().resolveRequiredPlaceholders(path);
126+
return getEnvironment().resolveRequiredPlaceholders(path); // 解析占位符 ${}
126127
}
127128

128129

spring-context/src/main/java/org/springframework/context/support/ClassPathXmlApplicationContext.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public ClassPathXmlApplicationContext(ApplicationContext parent) {
7676
}
7777

7878
/**
79+
* 我们手动创建容器一般会使用该方法,将传入一个 xml 文件的路径。
80+
*
7981
* Create a new ClassPathXmlApplicationContext, loading the definitions
8082
* from the given XML file and automatically refreshing the context.
8183
* @param configLocation resource location
@@ -124,6 +126,11 @@ public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh)
124126
}
125127

126128
/**
129+
* 真正执行的构造器方法,
130+
* configLocations:传入的 xml 文件路径,可以传入多个。
131+
* refresh:是否刷新容器,默认是 true
132+
* parent: 父容器
133+
*
127134
* Create a new ClassPathXmlApplicationContext with the given parent,
128135
* loading the definitions from the given XML files.
129136
* @param configLocations array of resource locations
@@ -138,10 +145,10 @@ public ClassPathXmlApplicationContext(
138145
String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
139146
throws BeansException {
140147

141-
super(parent);
142-
setConfigLocations(configLocations);
148+
super(parent); // 调用一系列父类的构造器,最终调到 AbstractApplicationContext 的构造器
149+
setConfigLocations(configLocations); // 设置 xml 文件的路径
143150
if (refresh) {
144-
refresh();
151+
refresh(); // 刷新容器
145152
}
146153
}
147154

0 commit comments

Comments
 (0)