1.嵌入式Servlet容器配置修改
-
- 1.通过全局配置文件修改
- 可以通过server.xxx 来进行web服务配置, 没有带服务器名称的则是通用配置
- 通过带了具体的服务器名称则是单独对该服务器进行设置,比如 server.tomcat.xxx 就是专门针对tomcat的配置
- 1.通过全局配置文件修改
-
- 2.通过WebServerFactoryCustomizer的Bean修改
- 修改server.xxx 配置的相关内容
- 会跟配置文件形成互补
- 2.通过WebServerFactoryCustomizer的Bean修改
@Component public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { @Override public void customize(ConfigurableServletWebServerFactory factory) { factory.setContextPath("/abc"); } }
2.注册servlet三大组件 servlet listener filter
方式一:servlet3.0规范提供的注解方式注册 需要使用 @ServletComponentScan 扫描才能生效
-
- @WebServlet
- @WebListener
- @WebFilter
@WebServlet(name="HelloServlet",urlPatterns = "/HelloServlet") //@WebFilter 使用方式相似 //@WebListener 使用方式相似 public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter writer = resp.getWriter(); writer.println("hello world 3333"); } }
@SpringBootApplication @ServletComponentScan public class SpringbootservletApplication { public static void main(String[] args) { SpringApplication.run(SpringbootservletApplication.class, args); } }
方式二:springboot 方式
public class BeanServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter writer = resp.getWriter(); writer.println("hello BeanServlet!"); } }
@Configuration public class SpringBootServlet { /** * SpringBoot提供的注册: * ServletRegistrationBean * FilterRegistrationBean * ServletListenerRegistrationBean */ @Bean public ServletRegistrationBean myServlet(){ ServletRegistrationBean<Servlet> servletServletRegistrationBean = new ServletRegistrationBean<>(); servletServletRegistrationBean.setServlet(new BeanServlet()); servletServletRegistrationBean.setName("BeanServlet"); servletServletRegistrationBean.addUrlMappings("/BeanServlet"); return servletServletRegistrationBean; } }
3.切换其他嵌入式Servlet容器
- Spring Boot包含对嵌入式Tomcat,Jetty和Undertow服务器的支持
- tomcat(默认)
- Jetty(socket)
- Undertow(响应式)
-
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--1.排除tomcat 要想使用jetty 或者 undertow 必须排除tomcat--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!--2.依赖jetty <dependency> <artifactId>spring-boot-starter-jetty</artifactId> <groupId>org.springframework.boot</groupId> </dependency>--> <!--3.依赖undertow <dependency> <artifactId>spring-boot-starter-undertow</artifactId> <groupId>org.springframework.boot</groupId> </dependency>-->
4.使用外部Servlet容器
1. 下载tomcat服务 springboot3.1.2 对应 Tomcat10.1.11 版本不对应会有问题,访问不了
2.设置当前maven项目的打包方式
<!--打包方式 默认是jar-->
<packaging>war</packaging>
3.让tomcat相关的依赖不参与打包部署 ,因为外置tomcat服务器已经有这些jar包
<!--让它不参与打包部署--> <dependency> <artifactId>spring-boot-starter-tomcat</artifactId> <groupId>org.springframework.boot</groupId> <scope>provided</scope> </dependency>
4. 为了让它支持springboot需要加上一下代码: 才能启动springboot应用
public class TomcatStartSpringBoot extends SpringBootServletInitializer{ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(SpringbootservletApplication.class); } }