SpringBoot进阶 (5)

2)定制错误的json数据

@RestControllerAdvice public class MyEceptionHandler { @ExceptionHandler(UserException.class) public String HandleException(Exception e){ return e.getMessage(); } }





14. 嵌入式Servlet容器

SpringBoot默认使用的是嵌入式的Servlet容器(Tomcat)


1)定制和修改Servlet容器的相关配置

server.port=8081 server.servlet.context-path=http://www.likecs.com/curd server.tomcat.uri-encoding=UTF-8 # 通用的Servlet容器设置 server.xxx server.tomcat.xxx
编写一个WebServerFactoryCustomizer:嵌入式的Servlet容器的定制器,来修改Servlet容器的配置 @Bean public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() { // 其实就是返回本类的实现类给他 return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() { @Override // 传参是一个容器 public void customize(ConfigurableWebServerFactory factory) { //设置tomcat的端口号 factory.setPort(9091); } }; }
重点

2)注册Servlet、Filter、Listener

ServletRegistrationBean

FilterRegistrationBean

SerlvetListenerRegistrationBean

Springboot默认是以jar包方式启动嵌入式的Servlet容器来启动应用,没有web.xml文件


编写Servlet

public class MySerlvet 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 World"); } }

使用官方提供的注册

package com.howl.springboot.config; @Configuration public class MyServerConfig { @Bean public ServletRegistrationBean myServlet(){ return new ServletRegistrationBean(new MySerlvet(),"/myServlet"); } @Bean public FilterRegistrationBean myFilter(){ FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(new MyFileter()); filterRegistrationBean.setUrlPatterns(Arrays.asList("/myServlet")); return filterRegistrationBean; } @Bean public ServletListenerRegistrationBean myListener(){ return new ServletListenerRegistrationBean<MyListener>(new MyListener()); } }
重点:使用其他的嵌入式Servlet容器(上面11点有提及怎么配置)

Jetty(长连接),聊天

Undertow(不支持JSP),非阻塞的,并发性能好

在pom.xml中,spring-boot-starter-web中默认加了spring-boot-starter-tomcat,所以我们要去除他,在加上spring-boot-starter-jetty

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
1) 嵌入式Servlet容器自动配置原理

autoConfigtuar.web.embedded下有个EmbeddedWebServerFactoryCustomizerAutoConfiguration(嵌入式容器工厂的定制器自动配置),其内部是有各种@ConditionalOnClass({Tomcat.class, UpgradeProtocol.class})条件判断,看容器中有哪个容器类类型才生效哪个配置返回一个TomcatWebServerFactoryCustomizer(Tomcat容器的定制器,传入ServerProperties.class)

注意:EmbeddedWebServerFactoryCustomizerAutoConfiguration(嵌入式容器工厂的定制器自动配置)

有@EnableConfigurationProperties({ServerProperties.class})注解,即与映射的配置文件绑定,即主配置文件中可修改

自动配置类中以tomcat为例

@Configuration @ConditionalOnClass({Tomcat.class, UpgradeProtocol.class}) public static class TomcatWebServerFactoryCustomizerConfiguration { public TomcatWebServerFactoryCustomizerConfiguration() {} @Bean public TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer(Environment environment, ServerProperties serverProperties) { // 将定制器返回,下面进入定制器 return new TomcatWebServerFactoryCustomizer(environment, serverProperties); } }
2)嵌入式容器启动原理 @SpringBootApplication public class SpringbootWebApplication { public static void main(String[] args) { SpringApplication.run(SpringbootWebApplication.class, args); } } SpringApplication类run方法中 先: context = this.createApplicationContext();创建IOC容器(有其他类型,举例WebApplication应用就创建WebApplication容器,下面有说明) 再this.refreshContext(context)刷新容器(这里就创建嵌入式容器,包括各种bean对象)

refreshContext一直往里走有这个方法,给子容器来实现

this.onRefresh(); // 而这个方法是给子容器实现的 protected void onRefresh() throws BeansException { }

子容器重写该方法:就会创建嵌入式的Servlet容器

子容器的抽象类下面的实现类,Tomcat是使用SerlvetWebServerApplicaitonContext


内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zydsdg.html