Skip to content

Commit 308e337

Browse files
committed
Polish "Disable XML reader when spring.xml.ignore is true"
See gh-22093
1 parent 8d5cf79 commit 308e337

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/BeanDefinitionLoader.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.beans.BeanUtils;
2727
import org.springframework.beans.factory.BeanDefinitionStoreException;
2828
import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
29+
import org.springframework.beans.factory.support.AbstractBeanDefinitionReader;
2930
import org.springframework.beans.factory.support.BeanDefinitionReader;
3031
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3132
import org.springframework.beans.factory.support.BeanNameGenerator;
@@ -59,20 +60,14 @@
5960
*/
6061
class BeanDefinitionLoader {
6162

62-
/**
63-
* Boolean flag controlled by a {@code spring.xml.ignore} system property that
64-
* instructs Spring to ignore XML, i.e. to not initialize the XML-related
65-
* infrastructure.
66-
* <p>
67-
* By default XML support is enabled.
68-
*/
69-
private static final boolean IS_XML_ENABLED = !SpringProperties.getFlag("spring.xml.ignore");
63+
// Static final field to facilitate code removal by Graal
64+
private static final boolean XML_ENABLED = !SpringProperties.getFlag("spring.xml.ignore");
7065

7166
private final Object[] sources;
7267

7368
private final AnnotatedBeanDefinitionReader annotatedReader;
7469

75-
private final XmlBeanDefinitionReader xmlReader;
70+
private final AbstractBeanDefinitionReader xmlReader;
7671

7772
private final BeanDefinitionReader groovyReader;
7873

@@ -91,7 +86,7 @@ class BeanDefinitionLoader {
9186
Assert.notEmpty(sources, "Sources must not be empty");
9287
this.sources = sources;
9388
this.annotatedReader = new AnnotatedBeanDefinitionReader(registry);
94-
this.xmlReader = (IS_XML_ENABLED ? new XmlBeanDefinitionReader(registry) : null);
89+
this.xmlReader = (XML_ENABLED ? new XmlBeanDefinitionReader(registry) : null);
9590
this.groovyReader = (isGroovyPresent() ? new GroovyBeanDefinitionReader(registry) : null);
9691
this.scanner = new ClassPathBeanDefinitionScanner(registry);
9792
this.scanner.addExcludeFilter(new ClassExcludeFilter(sources));
@@ -104,7 +99,7 @@ class BeanDefinitionLoader {
10499
void setBeanNameGenerator(BeanNameGenerator beanNameGenerator) {
105100
this.annotatedReader.setBeanNameGenerator(beanNameGenerator);
106101
this.scanner.setBeanNameGenerator(beanNameGenerator);
107-
if (IS_XML_ENABLED) {
102+
if (this.xmlReader != null) {
108103
this.xmlReader.setBeanNameGenerator(beanNameGenerator);
109104
}
110105
}
@@ -116,7 +111,7 @@ void setBeanNameGenerator(BeanNameGenerator beanNameGenerator) {
116111
void setResourceLoader(ResourceLoader resourceLoader) {
117112
this.resourceLoader = resourceLoader;
118113
this.scanner.setResourceLoader(resourceLoader);
119-
if (IS_XML_ENABLED) {
114+
if (this.xmlReader != null) {
120115
this.xmlReader.setResourceLoader(resourceLoader);
121116
}
122117
}
@@ -128,7 +123,7 @@ void setResourceLoader(ResourceLoader resourceLoader) {
128123
void setEnvironment(ConfigurableEnvironment environment) {
129124
this.annotatedReader.setEnvironment(environment);
130125
this.scanner.setEnvironment(environment);
131-
if (IS_XML_ENABLED) {
126+
if (this.xmlReader != null) {
132127
this.xmlReader.setEnvironment(environment);
133128
}
134129
}
@@ -182,8 +177,8 @@ private void load(Resource source) {
182177
this.groovyReader.loadBeanDefinitions(source);
183178
}
184179
else {
185-
if (!IS_XML_ENABLED) {
186-
throw new BeanDefinitionStoreException("Cannot load resources when XML support is disabled");
180+
if (this.xmlReader == null) {
181+
throw new BeanDefinitionStoreException("Cannot load XML bean definitions when XML support is disabled");
187182
}
188183
this.xmlReader.loadBeanDefinitions(source);
189184
}

0 commit comments

Comments
 (0)