Spring Boot

作者 : jamin 本文共3094个字,预计阅读时间需要8分钟 发布时间: 2020-10-18 共1256人阅读

Spring Boot

初遇 Spring Boot

Spring Boot 是 Spring MVC 的升级版,两者没有必然联系。

Spring Boot 的角色:Spring Framework -> Spring Boot -> Spring Cloud。

Spring Boot 的三大特性:

  1. 组件自动装配:Web MVC、Web Flux、JDBC 等
  2. 嵌入式 Web 容器:Tomcat、Jetty 以及 Undertow
  3. 生产准备特性:指标、健康检查、外部化配置等

组件自动装配

  1. 激活:@EnableAutoConfiguration
  2. 配置:/META-INF/spring.factories
  3. 实现:XXXAutoConfiguration

嵌入式 Web 容器

  1. Web Servlet:Tomcat、Jetty 和 Undertow
  2. Web Reactive:Netty Web Serve

生产准备特性

  1. 指标:/actuator/metrics
  2. 健康检查:/actuator/health
  3. 外部化配置:/actuator/configprops

启动方式

  1. mvn 启动:
mvn spring-boot:run
  1. 打成 jar 包启动:
# 打包
mvn clean package

# 启动
java -jar target/luckymoney-0.0.1-SNAPSHOT.jar

配置

基本配置

# 启动端口和路径
server:
  port: 3000
  servlet:
    context-path: /luckymoney

# 数据库和JPA
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 1124nicestar
    url: jdbc:mysql://127.0.0.1:3306/sell?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
  jpa:
    show-sql: true

自定义配置

  1. 单个引入
# 自定义配置
minMoney: 1
description: 最少金额${minMoney}元

在程序中引用自定义的配置参数:

@RestController
public class HelloController {

    @Value("${minMoney}")
    private BigDecimal minMoney;

    @Value("${description}")
    private String description;

    @GetMapping("/hello")
    public String sayHello() {
        return description;
    }
}
  1. 多个引入
limit:
  minMoney: 1
  maxMoney: 10
  description: 最少金额${limit.minMoney}元,最大金额${limit.maxMoney}元

提取配置:

@Data
@Component
@ConfigurationProperties(prefix = "limit")
public class LimitConfig {

    private BigDecimal minMoney;

    private BigDecimal maxMoney;

    private String description;
}

引入配置:

@RestController
public class HelloController {

    @Autowired
    private LimitConfig limitConfig;

    @GetMapping("/hello")
    public String sayHello() {
        return limitConfig.getDescription();
    }
}
  1. 区分开发与生产环境

application.yml 复制两份文件 application-dev.ymlapplication-prod.yml,然后再在 application.yml 中引入:

spring:
  profiles:
    active: dev

开发环境使用 dev,发布到线上时不需要更改,只需启动时加入参数启动:

java -jar -Dspring.profiles.active=prod target/luckymoney-0.0.1-SNAPSHOT.jar

注解

@RestController

@RestController = @Controller + @ResponseBody

@RestController 注解相当于 @Controller@ResponseBody 这两个注解的结合。

在使用 SpringMVC 框架的时候,在处理 json 的时候需要注解 @ResponseBody 或者 @RestController,这两个注解都会处理返回的数据格式,使用了该类型注解后返回的不再是视图,不会进行转跳,而是返回 json 或 xml 数据格式,输出在页面上。

所以在定义 Controller 的时候如果需要返回 jsp 界面就用 @Controller 注解,只需要返回 string 或 json 的时候就用 @RestController 注解。

两者区别:

@ResponseBody: 一般是使用在单独的方法上的,需要哪个方法返回 json 数据格式,就在哪个方法上使用,具有针对性。
@RestController:一般是使用在类上的,它相当于 @Controller@ResponseBody 这两个注解的结合,本质相当于在该类的所有方法上都统一使用了 @ResponseBody 注解。

@GetMapping

注解 @GetMapping 支持数组,多个路径可以访问同一个接口:

@GetMapping({"/hello", "/hi"})

获取路由参数有两种方式,一种是 /hello/200 路径,另外一种是 /hello?id=200 路径,方式分别如下:

// /hello/200
@GetMapping("/hello/{id}")
public String sayHello(@PathVariable("id") Integer id) {
    return "id:" + id;
}

// /hello?id=200
@GetMapping("/hello")
public String sayHello(@RequestParam("id") Integer id) {
    return "id:" + id;
}

更细致控制非必传和默认值:

@GetMapping("/hello")
public String sayHello(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id) {
    return "id:" + id;
}

@RequestParam 注解可以兼容 @PostMapping

@PostMapping("/hello")
public String sayHello(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id) {
    return "id:" + id;
}

Spring WebFlux

Spring5.x 是 Java 界首个支持响应式的 Web 框架,新增 Spring WebFlux 模块。

Spring WebFlux 同时支持使用旧的 Spring MVC 注解声明 Reactive Controller。和传统的 MVC Controller 不同,Reactive Controller 操作的是非阻塞ServerHttpRequestServerHttpResponse,而不再是 Spring MVC 里的 HttpServletRequestHttpServletResponse

本站所提供的部分资源来自于网络,版权争议与本站无关,版权归原创者所有!仅限用于学习和研究目的,不得将上述内容资源用于商业或者非法用途,否则,一切后果请用户自负。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源。如果上述内容资对您的版权或者利益造成损害,请提供相应的资质证明,我们将于3个工作日内予以删除。本站不保证所提供下载的资源的准确性、安全性和完整性,源码仅供下载学习之用!如用于商业或者非法用途,与本站无关,一切后果请用户自负!本站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。如有侵权、不妥之处,请联系站长以便删除!
金点网络 » Spring Boot

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
是否提供免费更新服务?
持续更新,永久免费
是否经过安全检测?
安全无毒,放心食用

提供最优质的资源集合

立即加入 友好社区
×