飞道的博客

SpringBoot总结(十三)——修改嵌入式Servlet容器配置

312人阅读  评论(0)

【第一部分】历史文章:
SpringBoot总结(一)——第一个SpringBoot项目
SpringBoot总结(二)——Spring Boot的自动配置
SpringBoot总结(三)——SpringBoot的配置文件
SpringBoot总结(四)——@Value和@ConfigurationProperties的区别
SpringBoot总结(五)——@PropertySource注解与@ImportResource注解
SpringBoot总结(六)——SpringBoot配置文件占位符
SpringBoot总结(七)——Profile的使用
SpringBoot总结(八)——配置文件的加载位置
SpringBoot总结(九)——@Conditional注解与自动配置报告
SpringBoot总结(十)——SpringBoot+Mybatis实现数据库的CRUD(从创建到实现【超详细附代码】)
SpringBoot总结(十一)——SpringBoot的静态资源映射规则
SpringBoot总结(十二)——登录界面的实现


我们知道:之前的web应用开发我们采取的方式是项目完成后打包成war包,然后配置tomcat启动运行项目,而SpringBoot默认使用的是嵌入式的tomcat,那我们需要如何配置嵌入式的Servlet容器呢?

  • (1)在配置文件中进行修改(在application.ropertiesapplication.yml配置文件中进行配置)
#配置端口号
server.port=8082

#配置项目的访问的路径
server.servlet.context-path=/hello

server.tomcat.uri-encoding=UTF-8

#禁止使用模板引擎的缓存
spring.thymeleaf.cache=false

注:可以通过查看ServerProperties类来修改具体参数。

  • (2)用编写配置类的方法。

SpringBoot1.xSpringBoot2.x版本中有所不同。在Spring Boot2.0中已经使用WebServerFactoryCustomizer取代了EmbeddedServletContainerCustomizer

在SpringBoot1.x中是这样使用的:

@Bean  
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
   
    return new EmbeddedServletContainerCustomizer() {
   
 
        //重写该方法
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
   
            container.setPort(8099);
        }
    };
}

SpringBoot2.x中是这样使用的:

package com.example.config;
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;
/**
 * @author 2017810402084
 */
@Configuration
public class MyConfig  {
   

    //配置嵌入式的Servlet容器
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory>   webServerFactoryWebServerFactoryCustomizer(){
   
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
   
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
   
                factory.setPort(8099);
            }
        };
    }
}


注册Servlet三大组件(Servlet、Filter过滤器、Listener监听器)
(1)Servlet。创建自定义类MyServlet继承HttpServlet。
示例代码:

package com.example.servlet;

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

/**
 * @author 2017810402084
 * (1)创建一个类继承HttpServlet
 * (2)添加到容器中
 *
 */
public class MyServlet extends HttpServlet {
   

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
        resp.getWriter().write("hello servlet...");
    }
}

把自定义的MyServlet添加到容器。

    //注册Servlet
    @Bean
    public ServletRegistrationBean  servletRegistrationBean(){
   
        ServletRegistrationBean<MyServlet> myServletServletRegistrationBean = new ServletRegistrationBean<>(new MyServlet(), "/");
        return myServletServletRegistrationBean;
    }

(2)Listener。创建自定义类MyListener,实现ServletContextListener接口。
示例代码:

package com.example.listener;


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

/**
 * @author 2017810402084
 */
public class MyListener  implements ServletContextListener {
   
    @Override
    public void contextInitialized(ServletContextEvent sce) {
   
        System.out.println("start......");

    }

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

    }
}

把自定义的MyListener 添加到容器。

    //注册Listener
    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean() {
   
        ServletListenerRegistrationBean<EventListener> eventListenerServletListenerRegistrationBean = new ServletListenerRegistrationBean<>(new MyListener());
        return eventListenerServletListenerRegistrationBean;
    }

(3)Filter。创建自定义类MyFilter实现Filter接口。
示例代码:

package com.example.filter;

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

/**
 * @author 2017810402084
 */
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("myfilter process....");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
   

    }
}

把自定义的MyFilter 添加到容器。

    //注册Filter拦截器
    @Bean
    public FilterRegistrationBean  filterRegistrationBean() {
   
        FilterRegistrationBean  filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new MyFilter());
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/filter","/wst"));
        return filterRegistrationBean;

    }

转载:https://blog.csdn.net/weixin_43759352/article/details/108424978
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场