我得到例外 -
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配置启用到项目中。 Spring会自动理解xml文件还是需要为它编写一些类? maven pom文件不包含sax依赖项。这是一个问题吗?
答案 0 :(得分:1)
我认为&#34; @ImportResource(&#34; classpath:log4j2.xml&#34;)&#34;是问题。请删除此行,然后重试。
使用@ImportResource,您可以添加一些Spring-XML-Configurations。这不适用于Log4J2-Configuration。
Log4J2-Framework将在类路径中搜索此资源(log4j2.xml)。请查看:http://logging.apache.org/log4j/2.x/manual/configuration.html
您必须使用Spring启用日志记录框架。请参阅http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html
答案 1 :(得分:0)
谢谢,大家为您解答。配置文件中有问题。经过一个小时的头脑风暴,我想出来了。刚刚更改了配置文件代码,它工作正常。