【问题标题】:Spring MVC Initializing Twice in Google AppEngineSpring MVC 在 Google AppEngine 中初始化两次
【发布时间】:2013-09-08 14:41:35
【问题描述】:

这个问题已经被问过了,我已经检查了所有的解决方案,但没有任何帮助,我仍然面临同样的问题。

我已将我的 Google Appengine 项目配置为使用 Spring MVC Using Java Config,并将 spring 的日志记录级别设置为 INFO 以检查初始化日志。

这是初始化时得到的,

    14:52:37,885 INFO  [org.springframework.web.servlet.DispatcherServlet] - FrameworkServlet 'SpringMvcConfig': initialization started
14:52:37,885 INFO  [org.springframework.web.servlet.DispatcherServlet] - FrameworkServlet 'SpringMvcConfig': initialization started
14:52:37,927 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] - Refreshing WebApplicationContext for namespace 'SpringMvcConfig-servlet': startup date [Sun Sep 08 14:52:37 UTC 2013]; root of context hierarchy
14:52:37,927 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] - Refreshing WebApplicationContext for namespace 'SpringMvcConfig-servlet': startup date [Sun Sep 08 14:52:37 UTC 2013]; root of context hierarchy
14:52:38,087 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] - JSR-330 'javax.inject.Named' annotation found and supported for component scanning
14:52:38,087 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] - JSR-330 'javax.inject.Named' annotation found and supported for component scanning
14:52:38,093 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] - Successfully resolved class for [com.test.config.SpringMvcConfig]
14:52:38,093 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] - Successfully resolved class for [com.test.config.SpringMvcConfig]
14:52:38,239 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] - JSR-330 'javax.inject.Named' annotation found and supported for component scanning
14:52:38,239 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] - JSR-330 'javax.inject.Named' annotation found and supported for component scanning
14:52:38,826 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
14:52:38,826 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
14:52:38,987 INFO  [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7fa6e654: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,springMvcConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,mainController,org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration,requestMappingHandlerMapping,mvcContentNegotiationManager,mvcConversionService,viewControllerHandlerMapping,beanNameHandlerMapping,resourceHandlerMapping,defaultServletHandlerMapping,requestMappingHandlerAdapter,mvcValidator,httpRequestHandler  Adapter,simpleControllerHandlerAdapter,handlerExceptionResolver,getInternalResourceViewResolver]; root of factory hierarchy
14:52:38,987 INFO  [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7fa6e654: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,springMvcConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,mainController,org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration,requestMappingHandlerMapping,mvcContentNegotiationManager,mvcConversionService,viewControllerHandlerMapping,beanNameHandlerMapping,resourceHandlerMapping,defaultServletHandlerMapping,requestMappingHandlerAdapter,mvcValidator,httpRequestHandlerAdapter,simpleControllerHandlerAdapter,handlerExceptionResolver,getInternalResourceViewResolver]; root of factory hierarchy
14:52:39,378 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Mapped "{[/ || /login],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto java.lang.String com.test.controller.MainController.hanldeRequest()
14:52:39,378 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Mapped "{[/ || /login],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto java.lang.String com.test.controller.MainController.hanldeRequest()
14:52:39,449 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] - Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]
14:52:39,449 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] - Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]
14:52:40,355 INFO  [org.springframework.web.servlet.DispatcherServlet] - FrameworkServlet 'SpringMvcConfig': initialization completed in 2468 ms
14:52:40,355 INFO  [org.springframework.web.servlet.DispatcherServlet] - FrameworkServlet 'SpringMvcConfig': initialization completed in 2468 ms

spring mvc初始化了两次,下面是我的配置

web.xml

 <servlet>
    <servlet-name>SpringMvcConfig</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextClass</param-name>
            <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            com.test.config.SpringMvcConfig
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>SpringMvcConfig</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

我的 Spring MVC 配置看起来像这样,

@Configuration
@EnableWebMvc
@ComponentScan("com.test.controller")
public class SpringMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false).favorParameter(true).ignoreAcceptHeader(true)
                .useJaf(false).defaultContentType(MediaType.APPLICATION_JSON).mediaType("json", MediaType.APPLICATION_JSON);
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(new CustomJacksonObjectMapper());
        converters.add(converter);
    }

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setCache(false);
        return resolver;
    }
}

谁能帮我解决这个问题?

任何解决方案或建议都会非常有帮助。

谢谢!

【问题讨论】:

  • 你能发布完整的日志吗?
  • 您确定不只是消息被记录了两次吗? (对我来说)这两个启动时间是相同的,这似乎非常可疑。
  • 我已经用完整的日志编辑了这个问题!
  • 是的,我也这么认为,但是 spring 框架如何记录两次呢?有没有可能发生?
  • 您是否配置了日志记录?如果有,怎么做?

标签: java spring google-app-engine spring-mvc


【解决方案1】:

如果我们查看您的 log4j.properties 文件

log4j.rootLogger=DEBUG, A1 

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout 
log4j.appender.A1.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p [%c] - %m%n 

log4j.logger.org.apache = WARN, A1 
log4j.logger.org.springframework = INFO, A1

org.springframework 的记录器和root 记录器都配置为使用A1 附加程序。如果您不将additivity 标志设置为false,它们都会追加。

Appender 可加性

logger C 的一条日志语句的输出会去 给 C 中的所有 appender 及其祖先。这是的意思 术语“附加程序可加性”。

但是,如果记录器 C 的祖先,比如 P,将可加性标志设置为 false,那么 C 的输出将被定向到 C 中的所有 appender 及其祖先,直到并包括 P,但不包括任何P的祖先。

默认情况下,Logger 的可加性标志设置为 true。

log4j.additivity.org.springframework=false

要么这样做,要么从 rootLogger 中删除 A1 附加程序

log4j.rootLogger=DEBUG

【讨论】:

    【解决方案2】:

    我在 tomcat 中遇到了非常相似的问题,因为上下文被加载了两次。一次由上下文加载侦听器(用于配置 servlet 过滤器),一次由调度 servlet。所以我想知道你是否正在做类似的事情?

    换句话说,默认情况下弹簧加载上下文中的某些内容。尝试禁用上下文的显式加载,看看会发生什么。

    在我的情况下,解决方案是分离上下文 - 过滤器和 servlet 具有不同的上下文。然后每个都正确加载,没有重叠。

    编辑:嗯,扩展 cmets 我看到你找到了类似的答案。我鼓励你更密切地跟进。你真的需要弄清楚是什么在加载额外的上下文。查找具有“特殊”名称的上下文文件(例如,以 servlet 或类似名称命名)。然后尝试更改他们的名字,例如。简而言之 - 你需要找到/打破自动加载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多