Provide a flag to disable XML support
sdeleuze opened this issue · 3 comments
Unlike other converters/codecs which can be enabled selectively based on classpath detection, XML ones are enabled by default in Spring MVC and WebFlux, but are also reachable in other places like properties loading facilities.
In order to improve effiency on both JVM and GraalVM native images for users not using XML (more common these days since JSON and Yaml are widely used), this issue plan to introduce a spring.xml.ignore
flag in order to disable XML support in a way that allows GraalVM native images to remove that code at build time.
Usage of XML support with the flag enabled should trigger a proper exception with a message explaining how to re-enable it.
For the record this allows to reduce native image size by 8.4M, RSS memory consumption by 4.2M and build time by 7s with Java 8.
The work done on spring-graalvm-native substitutions has allows to identify a first set of classes where XML support should be removed:
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
There are potential other places where we should seek where those XML related classes (used by GraalVM native to trigger XML support):
javax.xml.bind.Binder
javax.xml.datatype.DatatypeFactory
javax.xml.parsers.DocumentBuilderFactory
javax.xml.parsers.SAXParserFactory
javax.xml.stream.FactoryFinder
javax.xml.transform.FactoryFinder
org.w3c.dom.bootstrap.DOMImplementationRegistryClasses
Such flag could also be used on Spring Boot side:
- In
org.springframework.boot.BeanDefinitionLoader
to remove usage ofXmlBeanDefinitionReader
- In
org.springframework.boot.logging.logback.LogbackLoggingSystem
to remove XML support from Logback configuration.
Through this change we get an NPE in our JUnit 5 Test using the RestTemplate, because there are no properties loaded and no spring application properties should be loaded as well.
The problem is this change:
private static final boolean shouldIgnoreXml = SpringProperties.getFlag("spring.xml.ignore");
Exception:
java.lang.ExceptionInInitializerError
at com.xxx.xxx.RestConfigurationTest.testCompilerRestTemplate(RestConfigurationTest.java:14)
Caused by: java.lang.NullPointerException: Cannot invoke "java.util.Properties.getProperty(String)" because "org.springframework.core.SpringProperties.localProperties" is null