From cd42a8cee5a4ad66678ca181cc8416d1584e3507 Mon Sep 17 00:00:00 2001 From: withoutcause Date: Mon, 31 Aug 2020 22:41:58 +0800 Subject: [PATCH 1/2] =?UTF-8?q?ClassPathXmlApplicationContext=E6=9E=84?= =?UTF-8?q?=E9=80=A0=E5=99=A8=E7=9A=84=E5=89=8D=E4=B8=A4=E4=B8=AA=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E8=B0=83=E7=94=A8=E9=83=A8=E5=88=86=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../context/support/AbstractApplicationContext.java | 9 ++++++--- ...AbstractRefreshableConfigApplicationContext.java | 5 +++-- .../support/ClassPathXmlApplicationContext.java | 13 ++++++++++--- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java b/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java index 951861aa85b..33a13e31603 100644 --- a/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java @@ -215,6 +215,9 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader @Nullable private Thread shutdownHook; + /* + * 在 new 容器的时候,会将该资源初始化 + */ /** ResourcePatternResolver used by this context. */ private ResourcePatternResolver resourcePatternResolver; @@ -249,7 +252,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader * Create a new AbstractApplicationContext with no parent. */ public AbstractApplicationContext() { - this.resourcePatternResolver = getResourcePatternResolver(); + this.resourcePatternResolver = getResourcePatternResolver(); // 获取资源解析器 } /** @@ -258,7 +261,7 @@ public AbstractApplicationContext() { */ public AbstractApplicationContext(@Nullable ApplicationContext parent) { this(); - setParent(parent); + setParent(parent); // parent 默认为 null, 设置当前的 parent 参数为 null } @@ -489,7 +492,7 @@ LifecycleProcessor getLifecycleProcessor() throws IllegalStateException { * @see org.springframework.core.io.support.PathMatchingResourcePatternResolver */ protected ResourcePatternResolver getResourcePatternResolver() { - return new PathMatchingResourcePatternResolver(this); + return new PathMatchingResourcePatternResolver(this); // 该解析器支持 Ant 风格,简单的 url 协议。例如:file:// 这种也可以解析。 同时传入 resourceLoader } diff --git a/spring-context/src/main/java/org/springframework/context/support/AbstractRefreshableConfigApplicationContext.java b/spring-context/src/main/java/org/springframework/context/support/AbstractRefreshableConfigApplicationContext.java index 901e7152303..cca04495fad 100644 --- a/spring-context/src/main/java/org/springframework/context/support/AbstractRefreshableConfigApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/support/AbstractRefreshableConfigApplicationContext.java @@ -70,6 +70,7 @@ public void setConfigLocation(String location) { } /** + * @param locations 传入参数类型: classpath:/applicationContext.xml * Set the config locations for this application context. *

If not set, the implementation may use a default as appropriate. */ @@ -78,7 +79,7 @@ public void setConfigLocations(@Nullable String... locations) { Assert.noNullElements(locations, "Config locations must not be null"); this.configLocations = new String[locations.length]; for (int i = 0; i < locations.length; i++) { - this.configLocations[i] = resolvePath(locations[i]).trim(); + this.configLocations[i] = resolvePath(locations[i]).trim(); // 如果传入的是多个路径,向数组中添加值。 } } else { @@ -122,7 +123,7 @@ protected String[] getDefaultConfigLocations() { * @see org.springframework.core.env.Environment#resolveRequiredPlaceholders(String) */ protected String resolvePath(String path) { - return getEnvironment().resolveRequiredPlaceholders(path); + return getEnvironment().resolveRequiredPlaceholders(path); // 解析占位符 ${} } diff --git a/spring-context/src/main/java/org/springframework/context/support/ClassPathXmlApplicationContext.java b/spring-context/src/main/java/org/springframework/context/support/ClassPathXmlApplicationContext.java index 9b72875faa8..1dbe4b96286 100644 --- a/spring-context/src/main/java/org/springframework/context/support/ClassPathXmlApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/support/ClassPathXmlApplicationContext.java @@ -76,6 +76,8 @@ public ClassPathXmlApplicationContext(ApplicationContext parent) { } /** + * 我们手动创建容器一般会使用该方法,将传入一个 xml 文件的路径。 + * * Create a new ClassPathXmlApplicationContext, loading the definitions * from the given XML file and automatically refreshing the context. * @param configLocation resource location @@ -124,6 +126,11 @@ public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) } /** + * 真正执行的构造器方法, + * configLocations:传入的 xml 文件路径,可以传入多个。 + * refresh:是否刷新容器,默认是 true + * parent: 父容器 + * * Create a new ClassPathXmlApplicationContext with the given parent, * loading the definitions from the given XML files. * @param configLocations array of resource locations @@ -138,10 +145,10 @@ public ClassPathXmlApplicationContext( String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException { - super(parent); - setConfigLocations(configLocations); + super(parent); // 调用一系列父类的构造器,最终调到 AbstractApplicationContext 的构造器 + setConfigLocations(configLocations); // 设置 xml 文件的路径 if (refresh) { - refresh(); + refresh(); // 刷新容器 } } From 215712dfc3e667121fdb30c1bbcc0457befaca33 Mon Sep 17 00:00:00 2001 From: withoutcause Date: Mon, 31 Aug 2020 22:43:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Revert=20"ClassPathXmlApplicationContext?= =?UTF-8?q?=E6=9E=84=E9=80=A0=E5=99=A8=E7=9A=84=E5=89=8D=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E8=B0=83=E7=94=A8=E9=83=A8=E5=88=86=E6=B3=A8?= =?UTF-8?q?=E9=87=8A"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../context/support/AbstractApplicationContext.java | 9 +++------ ...AbstractRefreshableConfigApplicationContext.java | 5 ++--- .../support/ClassPathXmlApplicationContext.java | 13 +++---------- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java b/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java index 33a13e31603..951861aa85b 100644 --- a/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java @@ -215,9 +215,6 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader @Nullable private Thread shutdownHook; - /* - * 在 new 容器的时候,会将该资源初始化 - */ /** ResourcePatternResolver used by this context. */ private ResourcePatternResolver resourcePatternResolver; @@ -252,7 +249,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader * Create a new AbstractApplicationContext with no parent. */ public AbstractApplicationContext() { - this.resourcePatternResolver = getResourcePatternResolver(); // 获取资源解析器 + this.resourcePatternResolver = getResourcePatternResolver(); } /** @@ -261,7 +258,7 @@ public AbstractApplicationContext() { */ public AbstractApplicationContext(@Nullable ApplicationContext parent) { this(); - setParent(parent); // parent 默认为 null, 设置当前的 parent 参数为 null + setParent(parent); } @@ -492,7 +489,7 @@ LifecycleProcessor getLifecycleProcessor() throws IllegalStateException { * @see org.springframework.core.io.support.PathMatchingResourcePatternResolver */ protected ResourcePatternResolver getResourcePatternResolver() { - return new PathMatchingResourcePatternResolver(this); // 该解析器支持 Ant 风格,简单的 url 协议。例如:file:// 这种也可以解析。 同时传入 resourceLoader + return new PathMatchingResourcePatternResolver(this); } diff --git a/spring-context/src/main/java/org/springframework/context/support/AbstractRefreshableConfigApplicationContext.java b/spring-context/src/main/java/org/springframework/context/support/AbstractRefreshableConfigApplicationContext.java index cca04495fad..901e7152303 100644 --- a/spring-context/src/main/java/org/springframework/context/support/AbstractRefreshableConfigApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/support/AbstractRefreshableConfigApplicationContext.java @@ -70,7 +70,6 @@ public void setConfigLocation(String location) { } /** - * @param locations 传入参数类型: classpath:/applicationContext.xml * Set the config locations for this application context. *

If not set, the implementation may use a default as appropriate. */ @@ -79,7 +78,7 @@ public void setConfigLocations(@Nullable String... locations) { Assert.noNullElements(locations, "Config locations must not be null"); this.configLocations = new String[locations.length]; for (int i = 0; i < locations.length; i++) { - this.configLocations[i] = resolvePath(locations[i]).trim(); // 如果传入的是多个路径,向数组中添加值。 + this.configLocations[i] = resolvePath(locations[i]).trim(); } } else { @@ -123,7 +122,7 @@ protected String[] getDefaultConfigLocations() { * @see org.springframework.core.env.Environment#resolveRequiredPlaceholders(String) */ protected String resolvePath(String path) { - return getEnvironment().resolveRequiredPlaceholders(path); // 解析占位符 ${} + return getEnvironment().resolveRequiredPlaceholders(path); } diff --git a/spring-context/src/main/java/org/springframework/context/support/ClassPathXmlApplicationContext.java b/spring-context/src/main/java/org/springframework/context/support/ClassPathXmlApplicationContext.java index 1dbe4b96286..9b72875faa8 100644 --- a/spring-context/src/main/java/org/springframework/context/support/ClassPathXmlApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/support/ClassPathXmlApplicationContext.java @@ -76,8 +76,6 @@ public ClassPathXmlApplicationContext(ApplicationContext parent) { } /** - * 我们手动创建容器一般会使用该方法,将传入一个 xml 文件的路径。 - * * Create a new ClassPathXmlApplicationContext, loading the definitions * from the given XML file and automatically refreshing the context. * @param configLocation resource location @@ -126,11 +124,6 @@ public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) } /** - * 真正执行的构造器方法, - * configLocations:传入的 xml 文件路径,可以传入多个。 - * refresh:是否刷新容器,默认是 true - * parent: 父容器 - * * Create a new ClassPathXmlApplicationContext with the given parent, * loading the definitions from the given XML files. * @param configLocations array of resource locations @@ -145,10 +138,10 @@ public ClassPathXmlApplicationContext( String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException { - super(parent); // 调用一系列父类的构造器,最终调到 AbstractApplicationContext 的构造器 - setConfigLocations(configLocations); // 设置 xml 文件的路径 + super(parent); + setConfigLocations(configLocations); if (refresh) { - refresh(); // 刷新容器 + refresh(); } }