Skip to content

Commit c6594c2

Browse files
committed
Merge pull request #22093 from sdeleuze
* gh-22093: Polish "Disable XML reader when spring.xml.ignore is true" Disable XML reader when spring.xml.ignore is true Closes gh-22093
2 parents ea30c09 + 308e337 commit c6594c2

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
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;
3233
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
3334
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
3435
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
36+
import org.springframework.core.SpringProperties;
3537
import org.springframework.core.env.ConfigurableEnvironment;
3638
import org.springframework.core.io.ClassPathResource;
3739
import org.springframework.core.io.Resource;
@@ -53,17 +55,21 @@
5355
*
5456
* @author Phillip Webb
5557
* @author Vladislav Kisel
58+
* @author Sebastien Deleuze
5659
* @see #setBeanNameGenerator(BeanNameGenerator)
5760
*/
5861
class BeanDefinitionLoader {
5962

63+
// Static final field to facilitate code removal by Graal
64+
private static final boolean XML_ENABLED = !SpringProperties.getFlag("spring.xml.ignore");
65+
6066
private final Object[] sources;
6167

6268
private final AnnotatedBeanDefinitionReader annotatedReader;
6369

64-
private final XmlBeanDefinitionReader xmlReader;
70+
private final AbstractBeanDefinitionReader xmlReader;
6571

66-
private BeanDefinitionReader groovyReader;
72+
private final BeanDefinitionReader groovyReader;
6773

6874
private final ClassPathBeanDefinitionScanner scanner;
6975

@@ -80,10 +86,8 @@ class BeanDefinitionLoader {
8086
Assert.notEmpty(sources, "Sources must not be empty");
8187
this.sources = sources;
8288
this.annotatedReader = new AnnotatedBeanDefinitionReader(registry);
83-
this.xmlReader = new XmlBeanDefinitionReader(registry);
84-
if (isGroovyPresent()) {
85-
this.groovyReader = new GroovyBeanDefinitionReader(registry);
86-
}
89+
this.xmlReader = (XML_ENABLED ? new XmlBeanDefinitionReader(registry) : null);
90+
this.groovyReader = (isGroovyPresent() ? new GroovyBeanDefinitionReader(registry) : null);
8791
this.scanner = new ClassPathBeanDefinitionScanner(registry);
8892
this.scanner.addExcludeFilter(new ClassExcludeFilter(sources));
8993
}
@@ -94,8 +98,10 @@ class BeanDefinitionLoader {
9498
*/
9599
void setBeanNameGenerator(BeanNameGenerator beanNameGenerator) {
96100
this.annotatedReader.setBeanNameGenerator(beanNameGenerator);
97-
this.xmlReader.setBeanNameGenerator(beanNameGenerator);
98101
this.scanner.setBeanNameGenerator(beanNameGenerator);
102+
if (this.xmlReader != null) {
103+
this.xmlReader.setBeanNameGenerator(beanNameGenerator);
104+
}
99105
}
100106

101107
/**
@@ -104,8 +110,10 @@ void setBeanNameGenerator(BeanNameGenerator beanNameGenerator) {
104110
*/
105111
void setResourceLoader(ResourceLoader resourceLoader) {
106112
this.resourceLoader = resourceLoader;
107-
this.xmlReader.setResourceLoader(resourceLoader);
108113
this.scanner.setResourceLoader(resourceLoader);
114+
if (this.xmlReader != null) {
115+
this.xmlReader.setResourceLoader(resourceLoader);
116+
}
109117
}
110118

111119
/**
@@ -114,8 +122,10 @@ void setResourceLoader(ResourceLoader resourceLoader) {
114122
*/
115123
void setEnvironment(ConfigurableEnvironment environment) {
116124
this.annotatedReader.setEnvironment(environment);
117-
this.xmlReader.setEnvironment(environment);
118125
this.scanner.setEnvironment(environment);
126+
if (this.xmlReader != null) {
127+
this.xmlReader.setEnvironment(environment);
128+
}
119129
}
120130

121131
/**
@@ -167,6 +177,9 @@ private void load(Resource source) {
167177
this.groovyReader.loadBeanDefinitions(source);
168178
}
169179
else {
180+
if (this.xmlReader == null) {
181+
throw new BeanDefinitionStoreException("Cannot load XML bean definitions when XML support is disabled");
182+
}
170183
this.xmlReader.loadBeanDefinitions(source);
171184
}
172185
}

0 commit comments

Comments
 (0)