Spring Boot 不尊重 @WebServlet [英] Spring Boot does not honor @WebServlet
问题描述
我创建了一个 Servlet(从 HttpServlet 扩展)并按照 3.0 规范进行了注释
I created a Servlet (extending from HttpServlet) and annotated as per 3.0 specs with
@WebServlet(name="DelegateServiceExporter", urlPatterns={"/remoting/DelegateService"})
我在 Spring Boot 中的 @Configuration
类会扫描这个 servlet 的包.但是,当我的 Spring Boot 应用程序启动时,它没有记录它已经在嵌入式 Tomcat 8.0.15 容器中部署了这个 servlet.
My @Configuration
class in Spring Boot scans this servlet's package. However, it does not log that it has deployed this servlet in the embedded Tomcat 8.0.15 container when my Spring Boot application starts up.
因此,我也将 @Component
添加到了我的 servlet.现在,Spring Boot 注册 servlet(向我证明扫描包已正确设置),但随后它使用基于类名的 URL 模式使用驼峰式大小写注册它.所以,这更好 - 例如,我注册了一个 servlet,但 URL 映射错误!
So, I added @Component
to my servlet as well. Now, Spring Boot registers the servlet (proving to me that the scan package was correctly set up), but then it registers it with a URL pattern based on a class name using camel case. So, that was better - e.g., I got a servlet registered, but with the wrong URL mappings!
2015-01-05 11:29:08,516 INFO (localhost-startStop-1) [org.springframework.boot.context.embedded.ServletRegistrationBean] Mapping servlet: 'delegateServiceExporterServlet' to [/delegateServiceExporterServlet/]
如何让 Spring Boot 自动加载所有带有 @WebServlet
注释的 servlet 并遵守它们的 url 映射?
How do I get Spring Boot to auto-load all @WebServlet
annotated servlets and honor their url mappings?
推荐答案
在引导类中添加 @ServletComponentScan
.
比如
@SpringBootApplication
@ServletComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
这将使 Spring Boot 能够扫描 @WebServlet
以及 @WebListener
.
This will enable spring boot to scan @WebServlet
as well as @WebListener
.
这篇关于Spring Boot 不尊重 @WebServlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!