SpringBoot初识与使用

爱被打了一巴掌 2021-12-04 08:57 514阅读 0赞

##Spring特点简介

  1. Spring Boot是由Pivotal团队提供的全新框架
  2. 目的是用来简化Spring应用的初始搭建以及开发过程。
  3. SpringBoot提供了大量的启动器,帮助开发人员大大简化了配置。
  4. 从最根本上来讲,Spring Boot就是一些库的集合,它能够被任意项目的构建系统所使用。

##SpringBoot主要特点

  1. 创建独立的Spring应用程序,是一个基于spring容器来启动的spring应用
  2. 嵌入的Tomcat,无需部署WAR文件
  3. 简化Maven配置
  4. 自动配置Spring,autoConfiguration的功能。可以简化spring的配置信息。

##SpringBoot的使用

SpringBoot是一些库的集合,所以具有其独特的使用方式,即定义本地工程,并继承SpringBoot提供的父工程,实现资源的依赖。

  1. SpringBoot的静态资源目录默认在src/main/resources/static下。
  2. SpringBoot的模板页面资源目录默认在src/main/resources/templates

通过SpringBoot进行资源依赖的时候,都是依赖SpringBoot定义好的启动器,SpringBoot定义了44个启动器,诸如:spring-boot-starter-web、spring-boot-starter-jdbc、spring-boot-starter-test等。启动器中已经定义了完整的资源依赖信息,在工程中只要依赖少量的启动器,即可完成一个复杂工程的资源依赖。

spring-boot-starter-web启动器整合了Servlet技术。这个启动器包含一个Tomcat容器,可以通过SpringApplication来启动容器,提供WEB服务。这个容器的端口默认为8080,可以通过application.properties配置文件来修改端口。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.6.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>demo</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-devtools</artifactId>
  31. <scope>runtime</scope>
  32. <optional>true</optional>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-test</artifactId>
  37. <scope>test</scope>
  38. </dependency>
  39. </dependencies>
  40. <build>
  41. <plugins>
  42. <plugin>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-maven-plugin</artifactId>
  45. </plugin>
  46. </plugins>
  47. </build>
  48. </project>

启动类代码

  1. package com.example.demo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class DemoApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(DemoApplication.class, args);
  8. }
  9. }

控制代码

  1. package com.example.demo.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.ResponseBody;
  5. import java.util.Date;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. @Controller
  9. public class DemoController {
  10. @RequestMapping("test")
  11. @ResponseBody
  12. public Object test(){
  13. Map<String,Object> results=new HashMap<>();
  14. results.put("name", "张三");
  15. results.put("age", 20);
  16. results.put("date", new Date());
  17. return results;
  18. }
  19. }

image.png

##SpringBoot 在IDEA中实现热部署

  1. 在项目添加热部署插件

温馨提示:如果因为项目十分臃肿庞大,导致热重启很慢而影响开发效率,建议直接在在POM移除spring-boot-devtools依赖,然后使用Control+Shift+F9进行免启动快速更新

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-devtools</artifactId>
  4. <scope>runtime</scope>
  5. </dependency>
  1. 开启IDEA的自动编译(静态)

打开顶部工具栏 File -> Settings -> Build, Execution, Development -> Compiler 然后勾选 Build project automaticallyimage.png

  1. 开启IDEA的热部署策略

具体步骤:顶部菜单- >Edit Configurations->SpringBoot插件->目标项目->勾选热更新image.png

##SpringBoot的配置文件

  1. SpringBoot的配置文件默认放在src/resources/下
  1. server.port=8080
  1. SpringBoot的配置文件有两种格式
    a. application.properties (默认)
    b. application.yml (更简洁)
  1. server:
  2. port: 8080

配置不同环境下的配置文件

  1. #application.yml中配置
  2. spring:
  3. profiles:
  4. active: dev //那么application-dev.yml将生效
  5. spring:
  6. profiles:
  7. active: mo //那么application-mo.yml将生效
  8. spring:
  9. profiles:
  10. active: prod //那么application-prod.yml将生效

##附一个完整的配置文件

  1. server:
  2. port: 80
  3. servlet:
  4. session:
  5. timeout: 600
  6. max-http-header-size: 8192
  7. tomcat:
  8. uriEncoding: UTF-8
  9. compression:
  10. enabled: true
  11. mime-types: application/json,application/xml,text/html,text/xml,text/plain
  12. spring:
  13. application:
  14. name: xxx
  15. http:
  16. encoding:
  17. force: true
  18. enabled: true
  19. charset: UTF-8
  20. servlet:
  21. multipart:
  22. max-file-size: 200MB
  23. max-request-size: 200MB
  24. aop:
  25. proxy-target-class: true
  26. jmx:
  27. default-domain: ${spring.application.name}

发表评论

表情:
评论列表 (有 0 条评论,514人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringBoot

    信大家对SpringBoot的大名早有耳闻,那么他到底是什么呢?对于开发者而言,他有什么优点和优势呢?先来看看官方的介绍[SpringBoot官网][SpringBoot...

    相关 SpringBoot

    1. MVC范式 范式: 做某件事情的规范和模式! > MVC范式主要应用在WEB开发领域,是开发一个WEB应用必须遵守的规范和模式! MVC范式将web应用中,所有

    相关 SpringBoot

    Spring Boot的初体验和自动配置的探究 : 简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建

    相关 SpringBoot

          工作中一直有用到springboot,但是一直只是写业务代码,不了解其原理和框架的构建。最近终于有了充足的时间,小小的研究了一下springboot的真面目,下面是