zoukankan      html  css  js  c++  java
  • springboot开发之配置嵌入式Servlet容器两种方式

    springboot默认使用的是默认的Servlet容器(tomcat)。

    如何定制和修改Servlet容器的相关配置?

    (1)在主配置文件中修改与server的配置,例如server.port=8081等等

    (2)可以修改和tomcat相关的配置,使用server.tomcat.xxx

    (3)编写一个WebServerFactoryCustomizer:嵌入式的Servlet容器定制器,来修改Servlet容器的配置。我们可以建一个MyConfig.java

    package com.gong.springbootcurd.config;
    
    import com.gong.springbootcurd.component.LoginHandlerInterceptor;
    import com.gong.springbootcurd.component.MyLocaleResolver;
    import org.springframework.boot.web.server.ConfigurableWebServerFactory;
    import org.springframework.boot.web.server.WebServerFactory;
    import org.springframework.boot.web.server.WebServerFactoryCustomizer;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.LocaleResolver;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    //@EnableWebMvc 接管springmvc
    @Configuration
    public class MyMvcConfig implements WebMvcConfigurer{
    
        @Bean
        public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
            return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
                @Override
                public void customize(ConfigurableWebServerFactory factory) {
                    factory.setPort(8081);
                }
            };
        }
    }

    启动服务器之后会发现成功修改了servlet配置:

  • 相关阅读:
    将结构体存入Access数据库
    得到当前活动窗体的标题
    Scrapy各项命令说明
    session & viewstate
    网页设计中的默认字体样式详解
    ie6中href设为javascript:void(0)页面无法提交
    < ![if IE]> < ![endif]> 条件注释
    编译型与解释型、动态语言与静态语言、强类型语言与弱类型语言的区别
    Web字体的运用与前景
    jQuery和web.py美元符号($)冲突的解决方法
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12262971.html
Copyright © 2011-2022 走看看