• 配置log4j2日志报错 Cannot find the declaration of element 'Configuration'


    问题

    在配置log4j2的时候,在resources目录下添加了log4j2.xml,前两行的内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN">

    项目启动后报错500,报错信息如下:
    javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception

    org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 30; cvc-elt.1: Cannot find the declaration of element 'Configuration'.

    org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
    org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)

    HTTP Status 500 - Servlet.init() for servlet springmvc threw exception
    type Exception report
    
    message Servlet.init() for servlet springmvc threw exception
    
    description The server encountered an internal error that prevented it from fulfilling this request.
    
    exception
    
    javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception
        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
        org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
        org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
        org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2527)
        org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2516)
        java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        java.lang.Thread.run(Thread.java:748)
    root cause
    
    org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 2 in XML document from file [E:WorkspacesGitYonyougate	argetgate-0.1-SNAPSHOTWEB-INFclasseslog4j2.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 30; cvc-elt.1.a: Cannot find the declaration of element 'Configuration'.
        org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:405)
        org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:337)
        org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:305)
        org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
        org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:224)
        org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:195)
        org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
        org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
        org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:133)
        org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:637)
        org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:522)
        org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:702)
        org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:668)
        org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:716)
        org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:591)
        org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:530)
        org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:170)
        javax.servlet.GenericServlet.init(GenericServlet.java:158)
        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
        org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
        org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
        org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2527)
        org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2516)
        java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        java.lang.Thread.run(Thread.java:748)
    root cause
    
    org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 30; cvc-elt.1.a: Cannot find the declaration of element 'Configuration'.
        org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
        org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
        org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
        org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
        org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
        org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
        org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
        org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
        org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
        org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:77)
        org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadDocument(XmlBeanDefinitionReader.java:435)
        org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:393)
        org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:337)
        org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:305)
        org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
        org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:224)
        org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:195)
        org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
        org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
        org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:133)
        org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:637)
        org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:522)
        org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:702)
        org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:668)
        org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:716)
        org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:591)
        org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:530)
        org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:170)
        javax.servlet.GenericServlet.init(GenericServlet.java:158)
        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
        org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
        org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
        org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2527)
        org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2516)
        java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        java.lang.Thread.run(Thread.java:748)
    note The full stack trace of the root cause is available in the Apache Tomcat/8.0.47 logs.
    View Code

    走的弯路

    搜索网上资料,绝大部分的都是报错【spring cvc-elt.1: Cannot find the declaration of element 'beans'

    解决办法也都是,在xml的头文件中使用,把xsd文件的引用全部改为本地文件

    比如把

    xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      https://www.springframework.org/schema/context/spring-context.xsd 
      http://www.springframework.org/schema/mvc 
      https://www.springframework.org/schema/mvc/spring-mvc.xsd"


    改成:

    xsi:schemaLocation="http://www.springframework.org/schema/beans
      classpath:/org/springframework/beans/factory/xml/spring-beans.xsd
      http://www.springframework.org/schema/context
      classpath:/org/springframework/context/config/spring-context.xsd
      http://www.springframework.org/schema/mvc
      classpath:/org/springframework/web/servlet/config/spring-mvc.xsd"

    但是对于我来说并没有效果

    最终解决

    后来经过各种调整后发现,log4j2.xml配置的日志已经生效了,对应目录下会打印日志,但是启动后仍然报错。只要把log4j2.xml配置文件给删除掉,项目就可以正常运行。

    由此出发,再看日志,它是把 Configuration 当做了一个bean来对待了,但是它并不是一个bean

    最终解决,web.xml 里 修改。原来是:

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:*.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

    改成下面就好了,DispatcherServlet读取的时候,不读log4j.xml

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring.xml;classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

     原创文章,欢迎转载,转载请注明出处!

  • 相关阅读:
    python,os操作文件,文件路径(上一级目录)
    python屏幕的交互(读取输出信息)input,raw_input的区别
    Shell script 传参数处理(默认变量)
    python 2.4 的字符串转时间(日期减法取间隔时间)
    java的hashcode(结合hashset讲解)
    μCOS-II系统之事件(event)的使用规则及Semaphore的相互排斥量使用方法
    HDU 1079 Calendar Game (博弈论-sg)
    flume MemoryChannel 源代码解析
    MySQL查询时强制区分大写和小写
    蓝桥杯——真题训练之李白打酒
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/log4j_xml.html
Copyright © 2020-2023  润新知