SpringBoot内嵌入式的Servlet容器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Smiledmdm/article/details/83657090

由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。注册三大组件用以下方式ServletRegistrationBean,FilterRegistrationBean,ServletListenerRegistrationBean

@Configuration
public class MyServerConfig {
   //注册三大组件
    @Bean
    public ServletRegistrationBean myServlet(){
        //映射/myServlet请求
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
        registrationBean.setLoadOnStartup(1);
        return registrationBean;
    }
    @Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
        return registrationBean;
    }
    @Bean
    public ServletListenerRegistrationBean myListener(){
        ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
        return registrationBean;
    }
    //配置嵌入式的Servlet容器
    @Bean
    public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
        return new EmbeddedServletContainerCustomizer() {
            //定制嵌入式的Servlet容器相关的规则
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                container.setPort(8083);
            }
        };
    }
 
}
 


 

猜你喜欢

转载自blog.csdn.net/Smiledmdm/article/details/83657090
0条评论
添加一条新回复