diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/BeanDefinitionLoader.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/BeanDefinitionLoader.java index 9e29d1912b..bd8b850f61 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/BeanDefinitionLoader.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/BeanDefinitionLoader.java @@ -32,6 +32,7 @@ import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.context.annotation.AnnotatedBeanDefinitionReader; import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; +import org.springframework.core.SpringProperties; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; @@ -53,17 +54,27 @@ import org.springframework.util.StringUtils; * * @author Phillip Webb * @author Vladislav Kisel + * @author Sebastien Deleuze * @see #setBeanNameGenerator(BeanNameGenerator) */ class BeanDefinitionLoader { + /** + * Boolean flag controlled by a {@code spring.xml.ignore} system property that + * instructs Spring to ignore XML, i.e. to not initialize the XML-related + * infrastructure. + *
+ * By default XML support is enabled. + */ + private static final boolean IS_XML_ENABLED = !SpringProperties.getFlag("spring.xml.ignore"); + private final Object[] sources; private final AnnotatedBeanDefinitionReader annotatedReader; private final XmlBeanDefinitionReader xmlReader; - private BeanDefinitionReader groovyReader; + private final BeanDefinitionReader groovyReader; private final ClassPathBeanDefinitionScanner scanner; @@ -80,10 +91,8 @@ class BeanDefinitionLoader { Assert.notEmpty(sources, "Sources must not be empty"); this.sources = sources; this.annotatedReader = new AnnotatedBeanDefinitionReader(registry); - this.xmlReader = new XmlBeanDefinitionReader(registry); - if (isGroovyPresent()) { - this.groovyReader = new GroovyBeanDefinitionReader(registry); - } + this.xmlReader = (IS_XML_ENABLED ? new XmlBeanDefinitionReader(registry) : null); + this.groovyReader = (isGroovyPresent() ? new GroovyBeanDefinitionReader(registry) : null); this.scanner = new ClassPathBeanDefinitionScanner(registry); this.scanner.addExcludeFilter(new ClassExcludeFilter(sources)); } @@ -94,8 +103,10 @@ class BeanDefinitionLoader { */ void setBeanNameGenerator(BeanNameGenerator beanNameGenerator) { this.annotatedReader.setBeanNameGenerator(beanNameGenerator); - this.xmlReader.setBeanNameGenerator(beanNameGenerator); this.scanner.setBeanNameGenerator(beanNameGenerator); + if (IS_XML_ENABLED) { + this.xmlReader.setBeanNameGenerator(beanNameGenerator); + } } /** @@ -104,8 +115,10 @@ class BeanDefinitionLoader { */ void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; - this.xmlReader.setResourceLoader(resourceLoader); this.scanner.setResourceLoader(resourceLoader); + if (IS_XML_ENABLED) { + this.xmlReader.setResourceLoader(resourceLoader); + } } /** @@ -114,8 +127,10 @@ class BeanDefinitionLoader { */ void setEnvironment(ConfigurableEnvironment environment) { this.annotatedReader.setEnvironment(environment); - this.xmlReader.setEnvironment(environment); this.scanner.setEnvironment(environment); + if (IS_XML_ENABLED) { + this.xmlReader.setEnvironment(environment); + } } /** @@ -167,6 +182,9 @@ class BeanDefinitionLoader { this.groovyReader.loadBeanDefinitions(source); } else { + if (!IS_XML_ENABLED) { + throw new BeanDefinitionStoreException("Cannot load resources when XML support is disabled"); + } this.xmlReader.loadBeanDefinitions(source); } }