springboot概要

一、springboot的三大特性
1、组件自动装配:Web MVC、Web Flux、JDBC等
-激活:@EnableAutoConfiguration
-配置:/META-INF/spring.factories
-实现:XXXAutoConfiguration
2、嵌入式Web容器:Tomcat、Jetty以及UnderTow等
-Web Servlet: Tomcat、Jetty和Undertow
-Web Reactive:Netty Web Server
3、生产准备特性:指标、健康检查、外部化配置等
-指标:/actuator/metrics
-健康检查:/actuator/health
-外部化配置:/actuator/configprops
二、Web应用
1、传统Servlet应用
-Servlet组件:Servlet、Filter、Listener
-Servlet:实现、URL映射、注册
MyServlet.java

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * My Servlet
 */
@WebServlet(urlPatterns = "/my/servlet")
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        resp.getWriter().println("Hello,World!");

    }
}

SpringBootApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan(basePackages = "com.imooc.diveinspringboot.web.servlet")
public class DiveInSpringBootApplication {

	public static void main(String[] args) {
		SpringApplication.run(DiveInSpringBootApplication.class, args);
	}

}

-Servlet注册:Servlet注解、Spring Bean、RegistrationBean
-异步非阻塞:异步Servlet、非阻塞Servlet
MyServlet.java

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * My Servlet
 */
@WebServlet(urlPatterns = "/my/servlet",asyncSupported = true)
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        AsyncContext asyncContext = req.startAsync();
        asyncContext.start(()->{
            try {
                resp.getWriter().println("Hello,World!");
                //触发完成
                asyncContext.complete();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
}

2、Spring Web MVC应用
-Web MVC视图(viewResolver、view):模板引擎(Thymeleaf、Freemarker、JSP)、内容协商(ContentNegotiationConfigurer、ContentNegotiationStrategy、ContentNegotiatingViewResolver)、异常处理等(ExceptionHandler、HandlerExceptionResolver(ExceptionHandlerExceptionResolver)、BasicErrorController)
-Web MVC REST:资源服务(@RequestMapping(@GetMapping)、@ResponseBody、@RequestBody)、资源跨域(CrossOrigin、WebMvcConfigurer#addCorsMappings,传统解决方案(IFrame、JSONP))、服务发现(HATEOS)
-Web MVC核心:核心架构、处理流程、核心组件(DispatchServlet、HandlerMapping、HandlerAdapter、ViewResolver…)
3、Spring WebFlux应用 (since spring 5.x)
-Reactor基础:Java Lambda、Mono、Flux
-Web Flux核心:Web MVC注解兼容(@controller、@RequestMapping、@ResponseBody、@RequestBody…)、函数式声明(RouterFunction)、异步非阻塞(Servlet3.1 +、Netty Reactor)
-使用场景:页面渲染、Rest应用、性能测试
4、Web Server应用
-切换Web Server(将Tomcat切换为Jetty)
pom.xml

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<!--Exclude the Tomcat Dependency-->
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-tomcat</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<!--Use Jetty instead-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</artifactId>
		</dependency>

替换为WebFlux

	<!--<dependency>-->
			<!--<groupId>org.springframework.boot</groupId>-->
			<!--<artifactId>spring-boot-starter-web</artifactId>-->
			<!--<exclusions>-->
				<!--&lt;!&ndash;Exclude the Tomcat Dependency&ndash;&gt;-->
				<!--<exclusion>-->
					<!--<groupId>org.springframework.boot</groupId>-->
					<!--<artifactId>spring-boot-starter-tomcat</artifactId>-->
				<!--</exclusion>-->
			<!--</exclusions>-->
		<!--</dependency>-->

		<!--&lt;!&ndash;Use Jetty instead&ndash;&gt;-->
		<!--<dependency>-->
			<!--<groupId>org.springframework.boot</groupId>-->
			<!--<artifactId>spring-boot-starter-jetty</artifactId>-->
		<!--</dependency>-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webflux</artifactId>
		</dependency>

-自定义Servlet Web Server
webServletFactoryCustomizer
-自定义Reactive Web Server

三、数据相关
1、关系型数据
-JDBC:数据源,jdbcTemplate、自动装配
-JPA:实体映射关系、实体操作、自动装配
-事务:Spring事务抽象、JDBC事务处理、自动装配

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值