Provide a flag to disable XML support

This commit introduces a spring.xml.ignore system property
which when set to true avoid initializing XML infrastructure.

A typical use case is optimizing GraalVM native image footprint
for applications not using XML. In order to be effective, those
classes should be initialized at build time:

- org.springframework.util.DefaultPropertiesPersister
- org.springframework.core.io.support.PropertiesLoaderUtils
- org.springframework.web.servlet.function.support.RouterFunctionMapping
- org.springframework.web.client.RestTemplate
- org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver
- org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
- org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
- org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter
- org.springframework.http.codec.support.BaseDefaultCodecs
- org.springframework.beans.PropertyEditorRegistrySupport

Closes gh-25151
This commit is contained in:
Sébastien Deleuze
2020-06-19 08:54:21 +02:00
parent ce728a6c4b
commit 1e501f2583
10 changed files with 184 additions and 55 deletions

View File

@@ -24,6 +24,7 @@ import java.net.URLConnection;
import java.util.Enumeration;
import java.util.Properties;
import org.springframework.core.SpringProperties;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -41,6 +42,7 @@ import org.springframework.util.ResourceUtils;
*
* @author Juergen Hoeller
* @author Rob Harrop
* @author Sebastien Deleuze
* @since 2.0
* @see PropertiesLoaderSupport
*/
@@ -48,6 +50,13 @@ public abstract class PropertiesLoaderUtils {
private static final String XML_FILE_EXTENSION = ".xml";
/**
* 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.
* <p>The default is "false".
*/
private static final boolean shouldIgnoreXml = SpringProperties.getFlag("spring.xml.ignore");
/**
* Load properties from the given EncodedResource,
@@ -88,6 +97,9 @@ public abstract class PropertiesLoaderUtils {
try {
String filename = resource.getResource().getFilename();
if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
if (shouldIgnoreXml) {
throw new UnsupportedOperationException("XML support disabled");
}
stream = resource.getInputStream();
persister.loadFromXml(props, stream);
}
@@ -133,6 +145,9 @@ public abstract class PropertiesLoaderUtils {
try (InputStream is = resource.getInputStream()) {
String filename = resource.getFilename();
if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
if (shouldIgnoreXml) {
throw new UnsupportedOperationException("XML support disabled");
}
props.loadFromXML(is);
}
else {
@@ -180,6 +195,9 @@ public abstract class PropertiesLoaderUtils {
ResourceUtils.useCachesIfNecessary(con);
try (InputStream is = con.getInputStream()) {
if (resourceName.endsWith(XML_FILE_EXTENSION)) {
if (shouldIgnoreXml) {
throw new UnsupportedOperationException("XML support disabled");
}
props.loadFromXML(is);
}
else {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,8 @@ import java.io.Reader;
import java.io.Writer;
import java.util.Properties;
import org.springframework.core.SpringProperties;
/**
* Default implementation of the {@link PropertiesPersister} interface.
* Follows the native parsing of {@code java.util.Properties}.
@@ -46,6 +48,7 @@ import java.util.Properties;
* "defaultEncoding" and "fileEncodings" properties).
*
* @author Juergen Hoeller
* @author Sebastien Deleuze
* @since 10.03.2004
* @see java.util.Properties
* @see java.util.Properties#load
@@ -53,6 +56,14 @@ import java.util.Properties;
*/
public class DefaultPropertiesPersister implements PropertiesPersister {
/**
* 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.
* <p>The default is "false".
*/
private static final boolean shouldIgnoreXml = SpringProperties.getFlag("spring.xml.ignore");
@Override
public void load(Properties props, InputStream is) throws IOException {
props.load(is);
@@ -75,16 +86,25 @@ public class DefaultPropertiesPersister implements PropertiesPersister {
@Override
public void loadFromXml(Properties props, InputStream is) throws IOException {
if (shouldIgnoreXml) {
throw new UnsupportedOperationException("XML support disabled");
}
props.loadFromXML(is);
}
@Override
public void storeToXml(Properties props, OutputStream os, String header) throws IOException {
if (shouldIgnoreXml) {
throw new UnsupportedOperationException("XML support disabled");
}
props.storeToXML(os, header);
}
@Override
public void storeToXml(Properties props, OutputStream os, String header, String encoding) throws IOException {
if (shouldIgnoreXml) {
throw new UnsupportedOperationException("XML support disabled");
}
props.storeToXML(os, header, encoding);
}