首页 专题 文章 代码 归档
SpringBoot的嵌入式容器配置
2020.02.17 12:02 2020.02.17 12:47

1. 嵌入式容器配置

1、首先明白的一点的是:SpringBoot是默认采用ssm架构,里面的日志采用的是logback ,嵌入式的tomcat容器。

2、配置端口号,项目名等我们除了可以通过application.properties配置文件配置外,我们可以用代码自定义配置tomcat的相关设置。

1.1. Server的基本配置

1、通过配置文件配置端口号


#通用的Servlet容器设置
server.xxx
#Tomcat的设置
server.tomcat.xxx

server.port=8081
server.context-path=/crud
server.tomcat.uri-encoding=UTF-8

2、通过Java代码实现配置。-- WebServerFactoryCustomizer

package com.misiai.configure;

import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {
    //配置嵌入式的servlet容器
    @Bean//一定要将这个定制器加入到容器中
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryWebServerFactoryCustomizer() {
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                //定制嵌入式的Servlet容器相关的规则
                factory.setPort(1200);
            }
        };
    }

}

1.2. 注册Servlet三大组件

1.2.1. Servlet

定义继承自HttpServlet的类

package com.misiai.servlet;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // super.doGet(req, resp);
        resp.getWriter().write("hello");
    }
}

注册进容器

package com.misiai.config;

import com.misiai.service.HelloService;
import com.misiai.servlet.MyServlet;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Configuration 代表是一个配置类
 */
@Configuration
public class MyConfig {

    // 注册三大组件

    @Bean
    public ServletRegistrationBean myServlet() {
        return new ServletRegistrationBean(new MyServlet(), "/h2");

    }

}

访问:http://localhost:3000/h2

截图-1581220057

1.2.2. filter

filter

package com.misiai.filter;

import javax.servlet.*;
import java.io.IOException;

public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("My Filter Process");
        filterChain.doFilter(servletRequest,servletResponse); //放行
    }

    @Override
    public void destroy() {

    }
}

注册代码

    // 注册三大组件 - filter
    @Bean
    public FilterRegistrationBean<MyFilter> filterRegistrationBean(){
        FilterRegistrationBean<MyFilter> myFilterFilterRegistrationBean = new FilterRegistrationBean<>();
        myFilterFilterRegistrationBean.setFilter(new MyFilter());
//        myFilterFilterRegistrationBean.addUrlPatterns("/hello", "/myServlet");
        myFilterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/hello", "/h"));
        return myFilterFilterRegistrationBean;
    }
1.2.3. Listener

listener

package com.misiai.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("contextInitialized");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("contextDestroyed");
    }
}

注册代码

@Bean
public ServletListenerRegistrationBean<MyListener> servletListenerRegistrationBean() {
    return new ServletListenerRegistrationBean<MyListener>(new MyListener());
}
1.2.4. 使用注解

想要使用三大组件的注解,就必须先在SpringBoot主配置类(即标注了@SpringBootApplication注解的类)上标注@ServletComponentScan注解

@SpringBootApplication
@ServletComponentScan
// @ImportResource("classpath:beans.xml")
public class BootApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootApplication.class, args);
    }

}

配置Servlet

在我们自己的Servlet类上配置 @WebServlet(name = "cus", urlPatterns = "server") 注解即可

配置Filter

在我们自己的Filter上配置 @WebFilter(filterName = "fil", urlPatterns = {"/server", "/hello"}) 注解即可

配置Listener

在我们自己的Listener上配置 @WebListener 即可

2. 支持其他servlet容器

默认支持:

Tomcat(默认使用)

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   引入web模块默认就是使用嵌入式的Tomcat作为Servlet容器;
</dependency>

Jetty

<!-- 引入web模块 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <exclusions>
      <exclusion>
         <artifactId>spring-boot-starter-tomcat</artifactId>
         <groupId>org.springframework.boot</groupId>
      </exclusion>
   </exclusions>
</dependency>

<!--引入其他的Servlet容器-->
<dependency>
   <artifactId>spring-boot-starter-jetty</artifactId>
   <groupId>org.springframework.boot</groupId>
</dependency>

Undertow

<!-- 引入web模块 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <exclusions>
      <exclusion>
         <artifactId>spring-boot-starter-tomcat</artifactId>
         <groupId>org.springframework.boot</groupId>
      </exclusion>
   </exclusions>
</dependency>

<!--引入其他的Servlet容器-->
<dependency>
   <artifactId>spring-boot-starter-undertow</artifactId>
   <groupId>org.springframework.boot</groupId>
</dependency>
本节阅读完毕! (分享
二维码图片 扫描关注我们哟