SpringBoot嵌入式Web容器如何使用

发布时间:2023-01-12 10:26:00 作者:iii
来源:亿速云 阅读:114

这篇“SpringBoot嵌入式Web容器如何使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“SpringBoot嵌入式Web容器如何使用”文章吧。

嵌入式 Web 容器:应用中内置服务器(Tomcat),不用在外部配置服务器了

原理

SpringBoot嵌入式Web容器如何使用

ServletWebServerFactoryConfiguration.class

SpringBoot嵌入式Web容器如何使用

SpringBoot嵌入式Web容器如何使用

	@Override
	public WebServer getWebServer(ServletContextInitializer... initializers) {
		if (this.disableMBeanRegistry) {
			Registry.disableRegistry();
		}
		Tomcat tomcat = new Tomcat();
		File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat");
		tomcat.setBaseDir(baseDir.getAbsolutePath());
		Connector connector = new Connector(this.protocol);
		connector.setThrowOnFailure(true);
		tomcat.getService().addConnector(connector);
		customizeConnector(connector);
		tomcat.setConnector(connector);
		tomcat.getHost().setAutoDeploy(false);
		configureEngine(tomcat.getEngine());
		for (Connector additionalConnector : this.additionalTomcatConnectors) {
			tomcat.getService().addConnector(additionalConnector);
		}
		prepareContext(tomcat.getHost(), initializers);
		return getTomcatWebServer(tomcat);
	}

总结: 所谓内嵌服务器,就是把我们手动启动服务器的方法放进框架中了。

应用

1. 切换Web服务器

排除 tomcat 服务器,导入 undertow 依赖

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>

2. 定制服务器规则

方法一: 修改 server 下的配置文件

ServerProperties.class

SpringBoot嵌入式Web容器如何使用

server.undertow.accesslog.dir=/tmp

方法二: 自定义 ConfigurableServletWebServerFactory

方法三: 自定义 ServletWebServerFactoryCustomizer 定制化器

作用: 将配置文件的值,与 ServletWebServerFactory 绑定

SpringBoot 设计: Customizer 定制化器,可以定制 XXX 规则

以上就是关于“SpringBoot嵌入式Web容器如何使用”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

推荐阅读:
  1. 如何用springboot打包jar和war包
  2. springboot多环境配置yml文件版的具体操作

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot web

上一篇:SpringCloud hystrix断路器与局部降级问题怎么解决

下一篇:HTML5和CSS3怎么实现模态框

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》