SpringBoot 加载外部文件 报错jdk.internal.org.xml.sax.SAXParseException:

问题

package com.tangbaobao.springboot.sbtest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource("classpath:application.xml")
public class SbTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(SbTestApplication.class, args);
    }
}

在使用SpringBoot加载外部的配置文件的时候报错

Caused by: java.util.InvalidPropertiesFormatException: jdk.internal.org.xml.sax.SAXParseException; An XML properties document must contain the DOCTYPE declaration as defined by java.util.Properties.
	at java.base/jdk.internal.util.xml.PropertiesDefaultHandler.load(PropertiesDefaultHandler.java:85)
	at java.base/java.util.Properties.loadFromXML(Properties.java:962)
	at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:137)
	at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:122)
	at org.springframework.boot.env.PropertiesPropertySourceLoader.loadProperties(PropertiesPropertySourceLoader.java:58)
	at org.springframework.boot.env.PropertiesPropertySourceLoader.load(PropertiesPropertySourceLoader.java:47)
	at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadDocuments(ConfigFileApplicationListener.java:543)
	at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:498)
	... 23 common frames omitted
Caused by: jdk.internal.org.xml.sax.SAXParseException: An XML properties document must contain the DOCTYPE declaration as defined by java.util.Properties.
	at java.base/jdk.internal.util.xml.PropertiesDefaultHandler.startElement(PropertiesDefaultHandler.java:160)
	at java.base/jdk.internal.util.xml.impl.ParserSAX.parse(ParserSAX.java:470)
	at java.base/jdk.internal.util.xml.impl.ParserSAX.parse(ParserSAX.java:411)
	at java.base/jdk.internal.util.xml.impl.ParserSAX.parse(ParserSAX.java:374)
	at java.base/jdk.internal.util.xml.impl.SAXParserImpl.parse(SAXParserImpl.java:97)
	at java.base/jdk.internal.util.xml.PropertiesDefaultHandler.load(PropertiesDefaultHandler.java:83)
	... 30 common frames omitted

看看我的配置文件名称:
SpringBoot 加载外部文件 报错jdk.internal.org.xml.sax.SAXParseException:_第1张图片

原因

原因就在外部配置文件的命名上:
SpringBoot 加载外部文件 报错jdk.internal.org.xml.sax.SAXParseException:_第2张图片
springBoot启动的时候回加载classpath等目录下的以application开头的配置文件,默认为.properties和.yml,xml等,它解析的时候不会按照xml的格式解析,而是xsd的格式,所以会报错。

解决

更换外部配置文件名称,不叫application.xml

你可能感兴趣的:(SSM,issue)