SpringBoot 整合 SpringMVC:配置嵌入式服务器

修改和 server 相关的配置(ServerProperties):
XML 复制代码
server.port=8081
server.context‐path=/tx
server.tomcat.uri‐encoding=UTF‐8
  1. 注册 Servlet 三大组件:Servlet、Fileter、Listener
    1. SpringBoot 默认是以 jar 包的方式启动嵌入式的 Servlet 容器来启动 SpringBoot 的 web应用,没有外部文件

    2. Servlet:

      java 复制代码
      //注册三大组件
      @Bean
      public ServletRegistrationBean myServlet(){
          ServletRegistrationBean registrationBean = new ServletRegistrationBean(new
                  MyServlet(),"/myServlet");
          return registrationBean;
      }
    3. FilterRegisttratinBean:

      java 复制代码
      @Bean
      public FilterRegistrationBean myFilter(){
          FilterRegistrationBean registrationBean = new FilterRegistrationBean();
          registrationBean.setFilter(new MyFilter());
          registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
          return registrationBean;
      }
    4. SErvletListenerRegistrationBean:

      java 复制代码
      @Bean
      public ServletListenerRegistrationBean myListener(){
          ServletListenerRegistrationBean<MyListener> registrationBean = new
                  ServletListenerRegistrationBean<>(new MyListener());
          return registrationBean;
      }
    5. SpringBoot 帮我们自动整合 SpringMVC 的时候,自动的注册 SpringMVC 的前端控制器:DispatherServlet

    6. 在 DispatherServletAutoConfiguration 中:

      java 复制代码
      @Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
          @ConditionalOnBean(value = DispatcherServlet.class, name =
                  DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
          public ServletRegistrationBean dispatcherServletRegistration(
                  DispatcherServlet dispatcherServlet) {
              ServletRegistrationBean registration = new ServletRegistrationBean(
                      dispatcherServlet, this.serverProperties.getServletMapping());
      //默认拦截: / 所有请求;包静态资源,但是不拦截jsp请求; /*会拦截jsp
      //可以通过server.servletPath来修改SpringMVC前端控制器默认拦截的请求路径
              registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
              registration.setLoadOnStartup(
                      this.webMvcProperties.getServlet().getLoadOnStartup());
              if (this.multipartConfig != null) {
                  registration.setMultipartConfig(this.multipartConfig);
              }
              return registration;
          }

使用外置的 Servlet 容器:

  1. 嵌入式 Servlet 容器:应用打成可执行的 jar 包
  2. 优点:简单、便携
  3. 缺点:默认不支持 JSP、优化定制比较复杂
  4. 外置的 Servlet 容器:外面安装 Tomcat ,应用打包方式为 war 包
步骤:
  1. 创建一个 war 项目

  2. 将嵌入式的 Tomcat 指定为 provided

    XML 复制代码
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring‐boot‐starter‐tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
  3. 配置项目的目录结构:

  4. 部署 Tomcat

  5. 必须编写一个 SpringBootSerbletInitializer 的子类,并调用 configure 方法

    java 复制代码
    public class ServletInitializer extends SpringBootServletInitializer {
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(DemoApplication.class);
        }
    }
  6. 启动服务即可使用

原理:
  1. jar 包:执行 SpringBoot 主类的 main 方法,启动 IOC 容器,创建嵌入式的 Servlet 容器
  2. war 包:启动服务器,服务器启动 SpringBoot 应用[ SpringBootServletInitializer ],启动 IOC 容器
相关推荐
Hello World......27 分钟前
Java求职面试揭秘:从Spring到微服务的技术挑战
大数据·hadoop·spring boot·微服务·spark·java面试·互联网大厂
Hello World......33 分钟前
互联网大厂Java面试:从Spring到微服务的全面探讨
java·spring boot·spring cloud·微服务·面试·技术栈·互联网大厂
拾贰_C1 小时前
【SpringBoot】MyBatisPlus(MP | 分页查询操作
java·spring boot·后端·spring·maven·apache·intellij-idea
大大小小聪明4 小时前
Git合并多个提交方法详解
git·github
Uranus^4 小时前
深入解析Spring Boot与JUnit 5的集成测试实践
spring boot·单元测试·集成测试·junit 5·mockito
就叫飞六吧7 小时前
Spring Security 集成指南:避免 CORS 跨域问题
java·后端·spring
冼紫菜8 小时前
[特殊字符]CentOS 7.6 安装 JDK 11(适配国内服务器环境)
java·linux·服务器·后端·centos
Yvonne爱编码9 小时前
CSS- 4.1 浮动(Float)
前端·css·html·github·html5·hbuilder
秋野酱9 小时前
Spring Boot 项目的计算机专业论文参考文献
java·spring boot·后端
码视野9 小时前
基于Spring Boot和Vue的在线考试系统架构设计与实现(源码+论文+部署讲解等)
vue.js·spring boot·系统架构