Spring Boot自动注册servlet

版权声明:转载请注明链接 https://blog.csdn.net/weixin_38569499/article/details/89473500

    Spring Boot自动注册servlet的过程可以分成两个步骤:注册和扫描。

1、Servlet注册

    Servlet的注册是通过注解@WebServlet来实现的,可以在注解后面的括号中添加配置,例如:

@WebServlet(name = "virtual", urlPatterns = "/virtual", asyncSupported = true,
    description = "Servlet for virtual service")
public class VirtualWebServlet extends HttpServlet {

    其中,urlPatterns是Servlet的路径。

2、Servlet扫描

    Servlet扫描是在启动类中进行的,通过在启动类上面标注@ServletComponentScan注解开启Servlet的扫描功能。默认情况下,该注解会自动扫描启动类所在包及其子包中标注了@WebServlet的Servlet,如果servlet不在启动类的包或者子包中,就要去指定扫描路径,例如:

@SpringBootApplication(exclude = MongoAutoConfiguration.class,
    scanBasePackages = "com.test.be.virtual")
@ServletComponentScan("com.test.be.virtual.servlet")
public class VirtualServer {
  public static void main(String[] args) {
    SpringApplication.run(VirtualServer.class, args);
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_38569499/article/details/89473500