title: Why does loading log4j2.xml via Spring throw a NoClassDefFoundError for a log4j1 class?
body: <p>I'm deploying a Spring 4 web application to Tomcat 8 and am trying to load the log4j configuration (<code>log4j2.xml</code>) via Spring so that it can be stored outside of the WAR. With this configuration in place, Spring throws a <code>NoClassDefFoundError</code> for a log4j1 class (<code>org.apache.log4j.xml.DOMConfigurator</code>) during deployment. Log4j2 however is loaded correctly if it's on the classpath. I do not have logj1 libraries explicitly in my dependencies, only log4j2.</p>
  
  <p><a href="https://stackoverflow.com/questions/4400583/initializing-log4j-with-spring">Initializing Log4J with Spring?</a> proposes configuring log4j in web.xml. I tried this, but log4j is not loaded at all and no logging statements are printed from the application.</p>
  
  <p><a href="https://stackoverflow.com/questions/12275025/correctly-using-log4jconfigurer-in-spring">Correctly using Log4jConfigurer in Spring</a> proposes configuring a custom log4j config filename in Tomcat, but is not quite what I'm after. I need the file to be loaded from outside a WAR, given a filename.</p>
  
  <p>Spring configuration loading log4j2.xml:</p>
  
  <pre><code>&lt;!-- Load Log4j config file via Spring --&gt;
  &lt;bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"&gt;
      &lt;property name="targetClass" value="org.springframework.util.Log4jConfigurer"/&gt;
      &lt;property name="targetMethod" value="initLogging"/&gt;
      &lt;property name="arguments"&gt;
          &lt;list&gt;
              &lt;value&gt;${log4j.config.location}&lt;/value&gt;
              &lt;value&gt;${log4j.refresh.interval}&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Full stack trace:</p>
  
  <pre><code> org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'log4jInitialization' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/apache/log4j/xml/DOMConfigurator
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:684)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4760)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5184)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724)
      at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700)
      at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714)
      at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:919)
      at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1704)
      at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
      at java.util.concurrent.FutureTask.run(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/xml/DOMConfigurator
      at org.springframework.util.Log4jConfigurer.initLogging(Log4jConfigurer.java:102)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:269)
      at org.springframework.beans.factory.config.MethodInvokingBean.invokeWithTargetException(MethodInvokingBean.java:119)
      at org.springframework.beans.factory.config.MethodInvokingFactoryBean.afterPropertiesSet(MethodInvokingFactoryBean.java:106)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
  </code></pre>