springboot-配置1

springboot中配置:

  • 1-主要通过自己编写配置类(可继承xx配置类)并加上@Configuration注解,
  • 2-在自己写的配置类上编写方法通过@Bean注解返回对应配置类对象

配置嵌入式servlet容器

  • springboot2.x后
// 配置嵌入式的servlet容器
    @Bean
    public WebServerFactoryCustomizer webServerFactoryCustomizer(){ //
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {

            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                factory.setPort(8083);
            }
        };
    }

web三大组件配置

由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。
注册三大组件通过配置类中方法返回对应配置类实例.

  • servlet
    ServletRegistrationBean
--------自定义servlet
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 myservlet!!");
    }
}

----------配置类
@Bean
public ServletRegistrationBean myServlet(){
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyServlet(), "/myServlet");
    return servletRegistrationBean;
}
  • filter
    FilterRegistrationBean
--------自定义filter
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("MyFileter process....");
        filterChain.doFilter(servletRequest,servletResponse);
    }
    @Override
    public void destroy() {

    }
}

--------配置类
@Bean
public FilterRegistrationBean myFilter(){
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
    filterRegistrationBean.setFilter(new MyFilter());
    filterRegistrationBean.setUrlPatterns(Arrays.asList("/htllo","/myServlet"));
    return filterRegistrationBean;
}
  • listener
    ServletListenerRegistrationBean
---------自定义listener
public class MyListener implements ServletContextListener{

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("MyListener process.... contextInitialized..web启动!!!");
    }
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("MyListener process.... contextDestroyed..web销毁!!!");
    }
}

---------配置类
@Bean
public ServletListenerRegistrationBean myListener(){
    return new ServletListenerRegistrationBean(new MyListener());
}

----

SpringBoot帮我们自动SpringMVC的时候,自动的注册SpringMVC的前端控制器;DIspatcherServlet

@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServletRegistrationBean dispatcherServletRegistration(
      DispatcherServlet dispatcherServlet,
      WebMvcProperties webMvcProperties, 
      ObjectProvider<MultipartConfigElement> multipartConfig) {
      
      DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(
            dispatcherServlet,webMvcProperties.getServlet().getPath()); // 
      //默认拦截:/所有请求﹔包静态资源,但是不拦截jsp请求;/*会拦截jsp
      //可以按住ctrl后点击getPath()查看拦截路径,然后在配置文件中修改属性值以修改拦截的请求

      registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
      registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
      multipartConfig.ifAvailable(registration::setMultipartConfig);
      return registration;
}

切换其他servlet容器

默认支持:

  • tomcat(默认)
  • jetty(长连接)
  • undertow(不支持jsp)

切换servlet容器:
1-pom.xml文件排除默认tomcat容器

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

2-pom.xml文件引入需要用的容器坐标

  • 【jetty】
<!--引入其他的servlet容器-->
<!--jetty容器-->
<dependency>
    <artifactId>spring-boot-starter-jetty</artifactId>
    <groupId>org.springframework.boot</groupId>
</dependency>
  • 【undertow】
<!--jetty容器-->
<dependency>
    <artifactId>spring-boot-starter-undertow</artifactId>
    <groupId>org.springframework.boot</groupId>
</dependency>

嵌入式servlet容器配置原理

步骤:
1--SpringBoot根据导入的依赖情况,给容器中添加相应的EmbeddedServletContainerFactory 【TomcatEmbeddedServletContainerFactory]
2--容器中某个组件要创建对象就会惊动后置处理器;【EmbeddedServletContainerCustomizerBeanPostProcessor】只要是嵌入式的Servlet容器工厂,后置处理器就工作;
3--后置处理器,从容器中获取所有的【EmbeddedServletContainerCustomizer】调用定制器的定制方法,把配置配置到容器中
配置文件中的配置绑定【serverProperties】类,该类也是EmbeddedServletContainerCustomizer类型的定制器

原文地址:https://www.cnblogs.com/xiaoaiying/p/14221362.html