我收到異常,因爲進口log4j2.xml文件 -找不到元素「配置」的聲明,而在春天
INFO: Initializing Spring FrameworkServlet 'dispatcher' WARN Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 3 in XML document from class path resource [log4j2.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 31; Element type "Configuration" must be declared.
ERROR Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 3 in XML document from class path resource [log4j2.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 31; Element type "Configuration" must be declared.
下面是Spring註釋配置 -
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.test.*")
@PropertySource(value = {"classpath:application.properties"})
@ImportResource("classpath:log4j2.xml")
public class AppConfig extends WebMvcConfigurerAdapter {
...
}
下面是log4j2 xml配置文件 -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configuration>
<Configuration status="debug">
<Properties>
<Property name="log-path">C:/logs/</Property>
</Properties>
<Appenders>
...
</Appenders>
<Loggers>
...
</Loggers>
</Configuration>
如何擺脫這個錯誤?將文件導入應用程序時是否有任何錯誤?要將log4j2配置啓用到項目中。 xml文件會在春天自動理解,還是需要爲它編寫一些類? maven pom文件不包含sax依賴。那是問題嗎 ?
我認爲你不需要「@ImportResource(」classpath:log4j2.xml「)」和「<!DOCTYPE Configuration>」。請刪除此行,然後重試。 –
@TobiasOtto所做的更改如上所述,但日誌未顯示。 – Worker