簡體   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