【问题标题】:Spring methods being called twiceSpring方法被调用两次
【发布时间】:2013-12-31 10:10:42
【问题描述】:

我有一个 Spring,PrimeFaces JSF 应用程序配置为通过实现 WebApplicationInitializer 而不是 web.xml 的类加载应用程序(我仍然有一个裸露的 web.xml)。问题出在应用程序启动时,基本上在整个应用程序中,方法被调用了两次!我发现的最佳解释是可能会双重加载其中一位听众。我没有看到我正在这样做。下面附上我的WebApplicationInitializer 课程。我不确定还能提供什么来解决我的问题。甚至托管 bean 方法也被调用了两次。

public class WebAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

    final CharacterEncodingFilter cf = new CharacterEncodingFilter();
    cf.setEncoding("UTF-8");
    cf.setForceEncoding(true);

    servletContext.addListener(new RequestContextListener());

    servletContext
            .addFilter(
                    "ShiroFilter",
                    org.apache.shiro.web.servlet.IniShiroFilter.class)
            .addMappingForUrlPatterns(null, false, "/*");

    final WebApplicationContext context = getContext();
    servletContext.addListener(new ContextLoaderListener(context));

    final ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("*.do");
}

private AnnotationConfigWebApplicationContext getContext() {
    final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(AppConfig.class);

    return context;
}
}

我的web.xml 具有面部的标准定义和以下映射:

   .....
 <context-param>
    <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
    <param-value>true</param-value>
</context-param>
   .....
 <!-- JSF Mapping -->
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

当应用程序启动时,我看到 Spring 的输出也是重复的!

 2013-12-12T11:29:56.430-0500|INFO: [org.springframework.web.context.support.AnnotationConfigWebApplicationContext]  - Refreshing Root WebApplicationContext: startup date [Thu Dec 12 11:29:56 EST 2013]; root of context hierarchy

 2013-12-12T11:29:56.430-0500|INFO: [org.springframework.web.context.support.AnnotationConfigWebApplicationContext]  - Refreshing Root WebApplicationContext: startup date [Thu Dec 12 11:29:56 EST 2013]; root of context hierarchy

 2013-12-12T11:29:56.978-0500|INFO: [org.springframework.context.annotation.ClassPathBeanDefinitionScanner]  - JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning

 2013-12-12T11:29:56.978-0500|INFO: [org.springframework.context.annotation.ClassPathBeanDefinitionScanner]  - JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning

如何停止重复调用?谢谢。

编辑 我正在使用 Log4j。这是我的配置:

log4j.rootLogger=info, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%c] %x - %m%n
log4j.logger.org.hibernate=info, A1
log4j.logger.com.telus=info, A1
log4j.logger.org.springframework=info, A1

使用 Spring 3.1.4.RELEASE。 PrimeFaces 4.0。 JSF 2.1.7。部署在 Glassfish 3.Java 1.6.32 上。

【问题讨论】:

  • 这似乎是一个记录器问题。
  • 我可以尝试什么的任何指示?或者您需要更多信息?
  • 你使用什么日志框架? Log4j,logback,其他?发布您的记录器配置。
  • 我使用 Log4j 配置编辑了帖子。

标签: spring jsf servlets primefaces


【解决方案1】:

尝试将以下内容添加到您的log4j.properties

log4j.additivity.org.hibernate=false
log4j.additivity.com.telus=false
log4j.additivity.org.springframework=false

这应该确保日志消息不会被父根记录器复制。

【讨论】:

    猜你喜欢
    • 2014-09-02
    • 2017-02-15
    • 2021-11-04
    • 1970-01-01
    • 2016-09-29
    • 2014-12-15
    • 2020-07-16
    • 1970-01-01
    • 2014-05-22
    相关资源
    最近更新 更多