Skip to content

Commit 8d5cf79

Browse files
sdeleuzewilkinsona
authored andcommitted
Disable XML reader when spring.xml.ignore is true
This commit allows to set the XmlBeanDefinitionReader field from BeanDefinitionLoader to null in a way that allows the GraalVM native compiler to remove it from the native image when the spring.xml.ignore flag introduced by spring-projects/spring-framework#25151 is set to true. The purpose of this change is to allow smaller footprint on native images without requiring to use GraalVM native substitutions which are unmaintainable by nature and also to increase the consistency between JVM and native images. In order to effective, this optimization requires BeanDefinitionLoader class to be initialized at build time. See gh-22093
1 parent ea30c09 commit 8d5cf79

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
3333
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
3434
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
35+
import org.springframework.core.SpringProperties;
3536
import org.springframework.core.env.ConfigurableEnvironment;
3637
import org.springframework.core.io.ClassPathResource;
3738
import org.springframework.core.io.Resource;
@@ -53,17 +54,27 @@
5354
*
5455
* @author Phillip Webb
5556
* @author Vladislav Kisel
57+
* @author Sebastien Deleuze
5658
* @see #setBeanNameGenerator(BeanNameGenerator)
5759
*/
5860
class BeanDefinitionLoader {
5961

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");
70+
6071
private final Object[] sources;
6172

6273
private final AnnotatedBeanDefinitionReader annotatedReader;
6374

6475
private final XmlBeanDefinitionReader xmlReader;
6576

66-
private BeanDefinitionReader groovyReader;
77+
private final BeanDefinitionReader groovyReader;
6778

6879
private final ClassPathBeanDefinitionScanner scanner;
6980

@@ -80,10 +91,8 @@ class BeanDefinitionLoader {
8091
Assert.notEmpty(sources, "Sources must not be empty");
8192
this.sources = sources;
8293
this.annotatedReader = new AnnotatedBeanDefinitionReader(registry);
83-
this.xmlReader = new XmlBeanDefinitionReader(registry);
84-
if (isGroovyPresent()) {
85-
this.groovyReader = new GroovyBeanDefinitionReader(registry);
86-
}
94+
this.xmlReader = (IS_XML_ENABLED ? new XmlBeanDefinitionReader(registry) : null);
95+
this.groovyReader = (isGroovyPresent() ? new GroovyBeanDefinitionReader(registry) : null);
8796
this.scanner = new ClassPathBeanDefinitionScanner(registry);
8897
this.scanner.addExcludeFilter(new ClassExcludeFilter(sources));
8998
}
@@ -94,8 +103,10 @@ class BeanDefinitionLoader {
94103
*/
95104
void setBeanNameGenerator(BeanNameGenerator beanNameGenerator) {
96105
this.annotatedReader.setBeanNameGenerator(beanNameGenerator);
97-
this.xmlReader.setBeanNameGenerator(beanNameGenerator);
98106
this.scanner.setBeanNameGenerator(beanNameGenerator);
107+
if (IS_XML_ENABLED) {
108+
this.xmlReader.setBeanNameGenerator(beanNameGenerator);
109+
}
99110
}
100111

101112
/**
@@ -104,8 +115,10 @@ void setBeanNameGenerator(BeanNameGenerator beanNameGenerator) {
104115
*/
105116
void setResourceLoader(ResourceLoader resourceLoader) {
106117
this.resourceLoader = resourceLoader;
107-
this.xmlReader.setResourceLoader(resourceLoader);
108118
this.scanner.setResourceLoader(resourceLoader);
119+
if (IS_XML_ENABLED) {
120+
this.xmlReader.setResourceLoader(resourceLoader);
121+
}
109122
}
110123

111124
/**
@@ -114,8 +127,10 @@ void setResourceLoader(ResourceLoader resourceLoader) {
114127
*/
115128
void setEnvironment(ConfigurableEnvironment environment) {
116129
this.annotatedReader.setEnvironment(environment);
117-
this.xmlReader.setEnvironment(environment);
118130
this.scanner.setEnvironment(environment);
131+
if (IS_XML_ENABLED) {
132+
this.xmlReader.setEnvironment(environment);
133+
}
119134
}
120135

121136
/**
@@ -167,6 +182,9 @@ private void load(Resource source) {
167182
this.groovyReader.loadBeanDefinitions(source);
168183
}
169184
else {
185+
if (!IS_XML_ENABLED) {
186+
throw new BeanDefinitionStoreException("Cannot load resources when XML support is disabled");
187+
}
170188
this.xmlReader.loadBeanDefinitions(source);
171189
}
172190
}

0 commit comments

Comments
 (0)