SpringBoot学习笔记6web开发(6)嵌入式servlet容器操作

SpringBoot学习笔记—Day10

1.如何定制和修改servlet有关配置

在配置文件中, 配置server有关配置,其映射的类是ServerProperties,
例如:配置虚拟路径
server.servlet.context-path=/web
在配置类中添加这样一个组件一样可以达到效果
	@Bean
    public WebServerFactoryCustomizer webServerFactoryCustomizer(){
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                factory.setPort(8083);
            }
        };
    }

注册三大组件
1.注册servlet

 	@Bean
    public ServletRegistrationBean<HttpServlet> servletRegistrationBean(){
        return new ServletRegistrationBean<HttpServlet>(new MyServlet(),"/abc");
    }
	public class MyServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("啦啦啦啦啦");
    }
}

2.注册fifter

@Bean
    public FilterRegistrationBean<Filter> filterFilterRegistrationBean(){
        FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
        filterFilterRegistrationBean.setFilter(new MyFilter());
        filterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/hello","/abc"));
        return filterFilterRegistrationBean;
    }
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("filter执行了");
        chain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}

3.注册listener

@Bean
    public ServletListenerRegistrationBean<ServletContextListener> servletListenerRegistrationBean(){
        return new ServletListenerRegistrationBean<ServletContextListener>(new MyListener());
    }
public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("我的监听器启动了");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("我的监听器销毁了");
    }
}

另外springboot 自动配置SpringMVC的时候,帮我们自动配置 前端控制器DispacherServlet有dispatcherServletRegistration这样一个方法,就是返回的DispatcherServletRegistration类型,
DispatcherServletRegistration类继承的ServletRegistrationBean< DispatcherServlet>这个类

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(DispatcherServlet.class)
@AutoConfigureAfter(ServletWebServerFactoryAutoConfiguration.class)
public class DispatcherServletAutoConfiguration {

在DispatcherServletAutoConfiguration中

@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());
			registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
			registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
			multipartConfig.ifAvailable(registration::setMultipartConfig);
			return registration;
		}

在这行DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet,
webMvcProperties.getServlet().getPath());可以看到传输进去的拦截路径通过查找发现这个路径为"/",是在WebMvcProperties的静态内部类servlet中

public static class Servlet {

		/**
		 * Path of the dispatcher servlet.
		 */
		private String path = "/";

springboot1.0中是在serverProperties中,所以springboot1.0中是用可以用server.servletPath来修改前端控制器的拦截路径,但是在springboot2.0中,需要用spring.mvc.servlet.path来进行修改

spring.mvc.servlet.path=/  # springboot2.0的修改方式
server.servlet.path=/  # springboot1.0的修改方式

"/“是拦截所有请求,包括静态资源,但是不包括jsp请求,
如果是”/*"是拦截包括jsp在内的所有请求

2.springboot能不能支持其他的servlet容器

可以,默认支持tomcat,可以支持jetty,undertow

<!‐‐ 引入web模块 ‐‐> 
<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> 
<!‐‐引入其他的Servlet容器‐‐> 
<dependency> 
	<artifactId>spring‐boot‐starter‐jetty</artifactId> 
	<groupId>org.springframework.boot</groupId> 
</dependency>
<!‐‐ 引入web模块 ‐‐> 
<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> 
<!‐‐引入其他的Servlet容器‐‐> 
<dependency> 
	<artifactId>spring‐boot‐starter‐undertow</artifactId> 
	<groupId>org.springframework.boot</groupId> 
</dependency>

引入这俩个组件要先排除掉springboot默认的tomcat组件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值