繁体   English   中英

Spring 4 WebApplicationInitializer log4j2多个初始化问题

[英]Spring 4 WebApplicationInitializer log4j2 multiple initialization issues

我正在为Spring 4 Web应用程序使用100%代码配置方法构建应用程序。 以下是我的Web配置类。

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/*");
    }
}

问题-

一种。 我的四季豆初始化了两次

b。 每当我在我的资源中添加logj2.xml(使用Maven)时,我的bean创建都会失败。

我是新来的,请帮助我。

Log4J-2.5,Tomcat-8.0.32

谢谢!

我设法解决了。 这不是WebApplInitializer的问题,而是Spring Java Configurations文件的问题。 我正在为ApplicationContext和ServletContext维护单独的配置。 在ApplicationContext中,使用

@ComponentScan(value = "com.application.module", 
excludeFilters = {@ComponentScan.Filter(value = {Configuration.class})})

做到了。

在servlet上下文中,我使用-

@ComponentScan(basePackageClasses = AppContext.class)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM