当前位置: 首页 > wzjs >正文

石家庄seo网站优化价格营销推广活动方案

石家庄seo网站优化价格,营销推广活动方案,贵阳网站开发价格,网站分为哪几类58、嵌入式Servlet容器切换web服务器与定制化 # 嵌入式Servlet容器切换与定制化 ## 切换Web服务器 ### 1. 原理 Spring Boot默认使用Tomcat作为嵌入式Servlet容器。切换其他容器(如Jetty或Undertow)的原理如下: #### 自动配置类 - ServletWeb…

58、嵌入式Servlet容器切换web服务器与定制化

# 嵌入式Servlet容器切换与定制化

## 切换Web服务器

### 1. 原理

Spring Boot默认使用Tomcat作为嵌入式Servlet容器。切换其他容器(如Jetty或Undertow)的原理如下:

#### 自动配置类

- `ServletWebServerFactoryAutoConfiguration`是关键的自动配置类,负责创建`ServletWebServerFactory`。

#### 条件判断

- 根据项目中引入的依赖,自动配置类会判断系统中存在哪些Web服务器相关的类。

#### 工厂类

- Spring Boot提供了多个`ServletWebServerFactory`实现:

  - `TomcatServletWebServerFactory`

  

  - `JettyServletWebServerFactory`

  

  - `UndertowServletWebServerFactory`

- 根据条件判断,选择相应的工厂类创建对应的Web服务器。

### 2. 切换步骤

#### 排除默认Tomcat依赖

在`pom.xml`中排除`spring-boot-starter-tomcat`:

```xml

<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>

```

#### 添加目标服务器依赖

- **切换为Jetty**:

  ```xml

  <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-jetty</artifactId>

  </dependency>

  ```

- **切换为Undertow**:

  ```xml

  <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-undertow</artifactId>

  </dependency>

  ```

### 3. 示例

#### 切换为Jetty

```xml

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-web</artifactId>

    <exclusions>

        <!-- 排除Tomcat -->

        <exclusion>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-tomcat</artifactId>

        </exclusion>

    </exclusions>

</dependency>

<!-- 引入Jetty -->

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-jetty</artifactId>

</dependency>

```

重新启动应用,Spring Boot将使用Jetty作为嵌入式Servlet容器。

## 定制嵌入式Servlet容器

### 1. 通过配置文件

在`application.properties`或`application.yml`中配置服务器属性:

```properties

# 修改端口

server.port=8081

# 设置上下文路径

server.servlet.context-path=/myapp

# Tomcat特有配置

server.tomcat.uri-encoding=UTF-8

```

### 2. 实现定制器接口

实现`WebServerFactoryCustomizer`接口,定制`ServletWebServerFactory`:

```java

@Configuration

public class CustomServletContainerConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override

    public void customize(ConfigurableServletWebServerFactory factory) {

        // 设置端口

        factory.setPort(8082);

        

        // 设置访问日志

        factory.setAccessLogEnabled(true);

        

        // 其他定制...

    }

}

```

### 3. 示例

#### 定制Jetty容器

```java

@Configuration

public class JettyCustomizer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override

    public void customize(ConfigurableServletWebServerFactory factory) {

        if (factory instanceof JettyServletWebServerFactory) {

            JettyServletWebServerFactory jettyFactory = (JettyServletWebServerFactory) factory;

            

            // 设置Jetty特有配置

            jettyFactory.addServerCustomizers(server -> {

                // 添加连接器配置

                ServerConnector connector = new ServerConnector(server);

                connector.setPort(8083);

                server.addConnector(connector);

            });

        }

    }

}

```

通过以上方式,可以灵活地切换和定制Spring Boot的嵌入式Servlet容器,满足不同的应用需求。

http://www.dtcms.com/wzjs/175768.html

相关文章:

  • 营口网站制作网站排行榜查询
  • 晋城政府网站建设品牌推广策略有哪些
  • 做网站做的好的公司百度推广优化怎么做
  • 皮具网站设计哪个网站百度收录快
  • 网站建设首先要seo关键词优化培训
  • 石柱县建设局网站网络销售
  • 做网站要具备哪些2345网址导航主页
  • 网站外包多少人做百度新闻官网
  • 云营销网站建设电话咨询全达seo
  • 做网站数据库怎么整360seo排名点击软件
  • 邯郸市网站建设温州seo网站建设
  • 互联网保险的特点不包括优化大师win7官方免费下载
  • 做网站后台数据库建设品牌宣传推广文案
  • 使用html5做语音标注网站企业网络的组网方案
  • wordpress单本小说主题seo网络推广技术
  • 外贸网站建设费用多少域名注册查询网站
  • 上海武汉阳网站建设关键词排名代发
  • 做数学题网站关键词挖掘工具有哪些
  • 网站开发培训机构哪个好职业技能培训学校
  • 服装商城网站建设短视频seo询盘系统
  • idea可以做网站吗seo网站优化软件价格
  • 威县做网站哪儿好重庆森林经典台词截图
  • 手机网站添加微信方式中国法律服务网app最新下载
  • 个人怎么做音乐网站百度竞价培训
  • 信息发布的网站怎么做网络营销师证
  • 网站安全建设总结seo综合查询怎么关闭
  • w网站制作和推广seo优化网站推广全域营销获客公司
  • 惠州城乡和住房建设局网站佛山网站建设
  • 自适应网站设计尺寸百度推广竞价排名技巧
  • 网页搜索如何屏蔽广告福州360手机端seo