0

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

我已将我的 Google Appengine 项目配置为使用 Java Config 使用 Spring MVC,并将 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;
    }
}

谁能帮我解决这个问题?

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

谢谢!

4

2 回答 2

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

logger fororg.springframeworkrootlogger 都配置为使用A1appender。如果您不将additivity标志设置为false,它们都会追加。

附加程序可加性

logger C 的 log 语句的输出将发送到 C 中的所有 appender 及其祖先。这就是术语“附加程序可加性”的含义。

但是,如果记录器 C 的祖先(例如 P)将可加性标志设置为 false,则 C 的输出将定向到 C 中的所有 appender 及其祖先(包括 P),但不会定向到 P 的任何祖先中的 appender .

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

log4j.additivity.org.springframework=false

要么这样做,要么A1rootLogger

log4j.rootLogger=DEBUG
于 2013-09-08T21:34:30.323 回答
0

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

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

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

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

于 2013-09-08T16:14:34.193 回答