Spring 4 WebApplicationInitializer log4j2 multiple initialization issues
<p>I am building an application using 100% code configuration approach for a spring 4 web app. Following is my web config class.</p>

<pre><code>public class WebAppInitializer extends Log4jServletContainerInitializer implements WebApplicationInitializer {


    public void onStartup(ServletContext container) throws ServletException {
        super.onStartup(null, container);

        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(MyAppContext.class);

        // Manage the lifecycle of the root application context
        container.addListener(new ContextLoaderListener(rootContext));

        // Create the dispatcher servlet's Spring application context
        AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
        webContext.register(MyServletContext.class);

        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dynamic = container.addServlet("dispatcher", new DispatcherServlet(webContext));
        dynamic.setLoadOnStartup(1);
        dynamic.addMapping("/api/*");
    }
}
</code></pre>

<p>Problem - </p>

<p>a. My spring beans are getting initialized twice</p>

<p>b. Whenever I add logj2.xml in my resources (using maven), then my bean creation fails.</p>

<p>I am new to this, kindly help me. </p>

<p>Log4J - 2.5, Tomcat - 8.0.32</p>

<p>Thanks!</p>