SpringBoot special study Part18: SpringBoot server configuration parameters and three Web components (Servlet, Filter, Listener)

Brief

The usual web applications need to be labeled as war package and then configure the Tomcat container
and SpringBoot activated when the outside without Tomcat container
since SpringBoot using the built-in embedded Tomcat

In the starter spring-boot-starter-webinternal comes with spring-boot-starter-tomcatthat is embedded Tomcat


Server configuration parameters

If external Tomcat like corresponding configuration shall enter conf directory and configured in a configuration file
, then SpringBoot bottom and to modify the built-in Tomcat has a ServerProperties class
to modify the properties of this class which contains (e.g., port) in the configuration file is server parameters to be modified

Both methods can be modified:

1, modify the configuration file

E.g:

server.port=8081
server.servlet.context-path=/crud
Tomcat and modify the relevant configuration

ServerProperties class Tomcat which a man named object therefore:

server.tomcat.XXX=XXX

2, write a WebServerFactoryCustomizer: page Servlet factory customizer

Note : SpringBoot version 2.0 and above with the WebServerFactoryCustomizer
following versions with EmbeddedServletContainerCustomizerthe class

Servlet container by modifying the configuration of the method
configuration WebServerFactoryCustomizer you can write in your own configuration class (remember to add @Bean injection):

// 注入到Spring容器中
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer()
{
    return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
        // 定制嵌入式Servlet容器相关规则
        @Override
        public void customize(ConfigurableWebServerFactory factory) {
            factory.setPort(8082);
        }
    };
}

In fact, the underlying implementation of these two methods are the same
there will be many in SpringBoot in xxxCustomizer such names can be SpringBoot class by modifying these classes custom configuration


Registration of the three components

Three components are Servlet and Filter and Listener

Since SpringBoot is labeled jar package is not a standard directory structure of the web and no webapp folder
, if ever, then three components are registered in webapp / WEB-INF / web.xml in

Thus SpringBoot Providing three components respectively arranged in three ways:

  • ServletServletRegistrationBean
  • FilterFilterRegistrationBean
  • ListenerServletListenerRegistrationBean

✧, registration Servlet components:

To write a Servlet class of their own:

public class MyServlet extends HttpServlet {

    // 处理get请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    // 处理post请求
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("Hello Servlet!");
    }
}

Then configure through their own class to write the configuration container can be very convenient:

@Configuration
public class MyServerConfig {
    // 注册三大组件
    // 切记 要添加到容器中
    @Bean
    public ServletRegistrationBean myServlet()
    {
        ServletRegistrationBean<Servlet> ssrb = new ServletRegistrationBean<>(new MyServlet(),"/myServlet");
        return servletServletRegistrationBean;
    }
}

Of course, the same can also configure the startup sequence:
ssrb.setLoadOnStartup (. 1);
just like in the xml configuration file

✧, registered Filter components:

FIlter first to write a class of their own:

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

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("Hello Filter!");
        // 放行
        chain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}

Then configure the vessel:

@Configuration
public class MyServerConfig {
    @Bean
    public FilterRegistrationBean myFilter()
    {
        FilterRegistrationBean<Filter> ffrb = new FilterRegistrationBean<>();
        // 设置拦截器
        ffrb.setFilter(new MyFilter());
        // 设置要拦截的请求
        ffrb.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
        return ffrb;
    }
}

✧, registered Listener components:

Also the first to write a Listener own class:

public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        // ServletContext对象的初始化:Web应用的启动
        System.out.println("Hello Listener!");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // ServletContext对象的销毁:Web应用的关闭/销毁
        System.out.println("Bye Listener!");
    }
}

Then configure the vessel:

@Configuration
public class MyServerConfig {
    @Bean
    public ServletListenerRegistrationBean myListener()
    {
        ServletListenerRegistrationBean<MyListener> slrb = new ServletListenerRegistrationBean<>(new MyListener());
        return slrb;
    }
}

To configure is the same fool

SpringBoot when automatic configuration of SpringMVC has been automatically registered with the front controller (DispatcherServlet) front-end controller therefore would not have to manually register the
automatic registration of DispatcherServlet default is to intercept "/"that is, all requests also include static resources but does not intercept jsp requests
/*intercepts jsp but /not this is the difference

Profile by server.servletPathdefault request to modify the path to intercept the front controller SpringMVC

Example:

spring.mvc.servlet.path=/*

Published 174 original articles · won praise 5 · Views 240,000 +

Guess you like

Origin blog.csdn.net/Piconjo/article/details/104972303