博客
关于我
Spring Boot - Add a Servlet to an Application
阅读量:308 次
发布时间:2019-03-03

本文共 3877 字,大约阅读时间需要 12 分钟。

Spring Boot 环境下的 Servlet 配置与使用

准备

新建 Spring Boot Starter Project

  • 使用 IntelliJ IDEA 创建一个新的 Maven 项目
  • 添加 Spring Boot Starter Web 依赖到 pom.xml 中
  • org.springframework.boot
    spring-boot-starter-web
    1. 新建一个 Servlet 类,位于 src/main/java/com/mk/servlet/HelloServlet.java,内容如下:
    2. package com.mk.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class HelloServlet extends HttpServlet {    private static final long serialVersionUID = 1L;    public HelloServlet() {        System.out.println("HelloServlet.HelloServlet()");    }    @Override    public void init() throws ServletException {        System.out.println("HelloServlet.init()");    }    @Override    protected void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        System.out.println("HelloServlet.doGet()");        PrintWriter writer = response.getWriter();        writer.write("Hello, " + request.getRemoteAddr());        writer.flush();        writer.close();    }    @Override    public void destroy() {        System.out.println("HelloServlet.destroy()");    }}

      配置类

      创建一个配置类 ServletComponentConfiguration.java,位于 src/main/java/com/mk/configuration/ServletComponentConfiguration.java,内容如下:

      package com.mk.configuration;import org.springframework.boot.web.servlet.ServletComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ServletComponentScan(basePackages = {"com.mk.servlet"})public class ServletComponentConfiguration {}

      启动类

      创建一个启动类 SpringBootServletApplication.java,位于 src/main/java/com/mk/SpringBootServletApplication.java,内容如下:

      package com.mk;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SpringBootServletApplication {    public static void main(String[] args) {        SpringApplication.run(SpringBootServletApplication.class, args);    }}

      通过 Spring Bean 注册 Servlet

      编辑 ServletComponentConfiguration.java,添加以下代码:

      package com.mk.configuration;import java.util.Arrays;import javax.servlet.Servlet;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import com.mk.servlet.HelloServlet;@Configurationpublic class ServletComponentConfiguration {        @Bean    public ServletRegistrationBean servletRegistration() {        ServletRegistrationBean registration = new ServletRegistrationBean();        registration.setServlet(new HelloServlet());        registration.setUrlMappings(Arrays.asList("/hello"));        return registration;    }}

      启动应用后,控制台会输出:

      HelloServlet.HelloServlet()HelloServlet.init()

      表示 Servlet 已经成功注册到 Spring Boot 容器中。

      使用类路劲扫描注册 Servlet

      编辑 HelloServlet.java,添加 @WebServlet 注解:

      package com.mk.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet(urlPatterns = "/hello")public class HelloServlet extends HttpServlet {    // ... 保持不变}

      编辑 ServletComponentConfiguration.java,删除相关代码,并添加 @ServletComponentScan 注解:

      package com.mk.configuration;import org.springframework.boot.web.servlet.ServletComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ServletComponentScan(basePackages = {"com.mk.servlet"})public class ServletComponentConfiguration {}

      启动应用后,访问 /hello URL 应该能够正常工作。

      注意事项

    3. HelloServlet.init() 方法会在第一次访问时调用,后续不会重复调用
    4. 使用 @WebServlet 注解可以直接定义 URL 映射路径

    转载地址:http://jhyq.baihongyu.com/

    你可能感兴趣的文章
    Python 多处理不断产生 pythonw.exe 进程而不做任何实际工作
    查看>>
    Python 多处理从不加入
    查看>>
    Python 多处理如何优雅地退出?
    查看>>
    Python 多处理将子进程的标准输出重定向到 Tkinter 文本
    查看>>
    Python 多处理库错误(AttributeError:__exit__)
    查看>>
    Python 多处理模块的 .join() 方法到底在做什么?
    查看>>
    python 多线程与GIL
    查看>>
    Python 多线程学习(转)
    查看>>
    python 多进程-基础
    查看>>
    python 多进程-进阶-进程池
    查看>>
    python 多进程-进阶-进程间通信之Pipe
    查看>>
    python 多进程-进阶-进程间通信之Queue
    查看>>
    Python编程快速入门
    查看>>
    Python编程基础(附Pycharm与开发环境)
    查看>>
    python 如何“否定“value : 如果为真则返回假,如果为假则返回真
    查看>>