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组件