Home > Java > javaTutorial > How to use SpringBoot embedded web container

How to use SpringBoot embedded web container

PHPz
Release: 2023-05-13 23:34:04
forward
1504 people have browsed it

Embedded Web container: Built-in server (Tomcat) in the application, no need to configure the server externally

Principle

  • The SpringBoot project is started and it is found to be a web application. Introducing the web scenario package ----- For example: Tomcat

  • web application creates a web version of the IOC container ServletWebServerApplicationContext

  • ServletWebServerApplicationContext when started Looking for ServletWebServerFactory (Servlet's web server factory, used to produce Servlet servers)

  • ServletWebServerFactory There are many web server factories at the bottom by default

How to use SpringBoot embedded web container

  • #The bottom layer will be automatically configured, the automatic configuration class ServletWebServerFactoryAutoConfiguration

  • ServletWebServerFactoryAutoConfiguration imports the ServletWebServerFactoryConfiguration factory configuration class

ServletWebServerFactoryConfiguration.class

How to use SpringBoot embedded web container

  • Dynamicly determine which web server configuration package is imported into the system

  • If Importing Tomcat dependencies will automatically place a Tomcat server factory. TomcatServletWebServerFactory creates a Tomcat server factory for us

  • Tomcat bottom layer supports the following servers

How to use SpringBoot embedded web container

	@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);
	}
Copy after login

Summary: The so-called embedded server is to put our method of manually starting the server into the framework.

Application

1. Switch Web server

Exclude tomcat server and import undertow dependency

       <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>
Copy after login

2. Customize server rules

Method One: Modify the configuration file under the server

ServerProperties.class

How to use SpringBoot embedded web container

##server.undertow.accesslog.dir=/tmp

Method 2: Customize ConfigurableServletWebServerFactory

Method 3: Customize ServletWebServerFactoryCustomizer Customizer

Function: Bind the value of the configuration file to ServletWebServerFactory

SpringBoot Design: Customizer, you can customize XXX rules

The above is the detailed content of How to use SpringBoot embedded web container. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template