【发布时间】:2018-09-24 09:46:54
【问题描述】:
我有一个运行 Kafka 和 JPA 的 Spring Boot 应用程序。我想添加一个管理页面,所以从添加“spring-boot-starter-web”和添加控制器类开始。但是,当我启动我的应用程序时,我可以看到 Tomcat 服务器已启动并且 dispatcherServlet 已初始化。
2018-04-13 18:25:29.495 INFO 5512 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$f5f4a697] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-04-13 18:25:30.584 INFO 5512 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2018-04-13 18:25:30.604 INFO 5512 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-04-13 18:25:30.607 INFO 5512 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.27
2018-04-13 18:25:33.052 INFO 5512 --- [ost-startStop-1] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded
JARs during scanning can improve startup time and JSP compilation time.
2018-04-13 18:25:33.384 INFO 5512 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-04-13 18:25:33.384 DEBUG 5512 --- [ost-startStop-1] o.s.web.context.ContextLoader : Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
2018-04-13 18:25:33.385 INFO 5512 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization comp
leted in 6776 ms
2018-04-13 18:25:33.761 INFO 5512 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2018-04-13 18:25:33.768 INFO 5512 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'metricsFilter' to: [/*]
但我看不到 Controller 类使用 handlerMapping 进行映射。调用 http://localhost:8080 只会给我一个服务器不可用错误。
我该如何调试呢?我已经检查了以下内容:
- Controller 类位于主 Application.java 的子文件夹中 文件。
- Controller 有@Controller 注解。该包中的其他@Components 正在自动装配,所以我确信该文件夹是 扫描。
- 我在主类中扩展了 SpringBootServletInitializer 类。
- 主类中有如下注解 -@SpringBootApplication @Import(PersistenceConfig.class)、@EnableKafka、@EnableCaching、@EnableWebMvc、 @EnableAutoConfiguration(排除 = { KafkaAutoConfiguration.class }) 这里 PersistenceConfig 是我自己的类。
有没有办法找出为什么 MVC 没有将 Controller 类添加到 URL 映射?
我不能分享代码,因为它不是公开的。感谢您对此进行调查。
【问题讨论】:
-
您可以通过创建mvce来共享代码
-
@AbhijitSarkar 我可以使用 JSP 和 tomcat 获得简单的 Spring Boot 应用程序。问题在于我现有的项目有很多组件 - Kafka 消费者/生产者、JPA 等。如果有办法跟踪组件扫描或 MVC 初始化过程,我想我会有所了解。
-
可以得到applicationContext = SpringApplication.run(Application.class, args);然后 String[] allBeanNames = applicationContext.getBeanDefinitionNames();查看是否已加载。或者使用 actuator
Spring Boot Actuator 功能提供了用于监控我们应用程序的端点统计数据。org.springframework.boot spring-boot-starter-actuator -
@VikramPalakurthi 谢谢。你的回答让我更接近这个问题。当我尝试打印 bean 名称时,我看到 run() 方法没有返回 bean 名称,因为上下文一直在加载。问题是有一个 bean 在启动时加载数据,因此上下文尚未准备好为映射 URL 的请求提供服务。
标签: spring-mvc tomcat spring-boot