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

【第一部分】历史文章:
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;

    }
  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小Java开发者

“是一种鼓励,你懂的”

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值