2016-11-25 116 views
1

我收到異常,因爲進口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依賴。那是問題嗎 ?

+0

我認爲你不需要「@ImportResource(」classpath:log4j2.xml「)」和「<!DOCTYPE Con​​figuration>」。請刪除此行,然後重試。 –

+0

@TobiasOtto所做的更改如上所述,但日誌未顯示。 – Worker

回答

1

我認爲「@ImportResource(」classpath:log4j2.xml「)」是問題所在。請刪除此行並重試。

使用@ImportResource可以添加一些Spring-XML配置。這不適用於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

+0

那麼如何導入log4j2 xml文件?沒有這個XML文件日誌將不會生成。我只想用xml文件進行配置。 – Worker

+0

謝謝你的回答。 – Worker

0

謝謝大家爲你解答。在配置文件中有問題。經過一個小時的頭腦風暴,我找到了答案。只是改變了配置文件代碼,它工作正常。

相關問題