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
pull/22113/head
Andy Wilkinson 4 years ago
commit c6594c2b7e

@ -26,12 +26,14 @@ import groovy.lang.Closure;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
import org.springframework.beans.factory.support.AbstractBeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
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 +55,21 @@ import org.springframework.util.StringUtils;
*
* @author Phillip Webb
* @author Vladislav Kisel
* @author Sebastien Deleuze
* @see #setBeanNameGenerator(BeanNameGenerator)
*/
class BeanDefinitionLoader {
// Static final field to facilitate code removal by Graal
private static final boolean XML_ENABLED = !SpringProperties.getFlag("spring.xml.ignore");
private final Object[] sources;
private final AnnotatedBeanDefinitionReader annotatedReader;
private final XmlBeanDefinitionReader xmlReader;
private final AbstractBeanDefinitionReader xmlReader;
private BeanDefinitionReader groovyReader;
private final BeanDefinitionReader groovyReader;
private final ClassPathBeanDefinitionScanner scanner;
@ -80,10 +86,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 = (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 +98,10 @@ class BeanDefinitionLoader {
*/
void setBeanNameGenerator(BeanNameGenerator beanNameGenerator) {
this.annotatedReader.setBeanNameGenerator(beanNameGenerator);
this.xmlReader.setBeanNameGenerator(beanNameGenerator);
this.scanner.setBeanNameGenerator(beanNameGenerator);
if (this.xmlReader != null) {
this.xmlReader.setBeanNameGenerator(beanNameGenerator);
}
}
/**
@ -104,8 +110,10 @@ class BeanDefinitionLoader {
*/
void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
this.xmlReader.setResourceLoader(resourceLoader);
this.scanner.setResourceLoader(resourceLoader);
if (this.xmlReader != null) {
this.xmlReader.setResourceLoader(resourceLoader);
}
}
/**
@ -114,8 +122,10 @@ class BeanDefinitionLoader {
*/
void setEnvironment(ConfigurableEnvironment environment) {
this.annotatedReader.setEnvironment(environment);
this.xmlReader.setEnvironment(environment);
this.scanner.setEnvironment(environment);
if (this.xmlReader != null) {
this.xmlReader.setEnvironment(environment);
}
}
/**
@ -167,6 +177,9 @@ class BeanDefinitionLoader {
this.groovyReader.loadBeanDefinitions(source);
}
else {
if (this.xmlReader == null) {
throw new BeanDefinitionStoreException("Cannot load XML bean definitions when XML support is disabled");
}
this.xmlReader.loadBeanDefinitions(source);
}
}

Loading…
Cancel
Save