当前位置: 首页 > news >正文

第一章 SpringBoot 介绍-最小配置

1. spring-boot web应用的最小配置

1.1 pom配置两个依赖

  	<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>

1.2 新建一个启动类

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

1.3 新建一个controller

@RestController
public class HelloController {@RequestMapping("/hello")public String handle01(){return "Hello, Spring Boot 2!";}
}

1.4 请求地址:localhost:8080/hello

1.5 扩展

1、maven命令:mvn install,会将项目打包成原始的jar,不能直接启动
2、如果需要将带main方法的maven项目打包成可运行的jar包,需要pom引入组件<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
3、项目根目录下打包命令 mvn install
4、运行命令 java -jar target/springbootinitial-1.0-SNAPSHOT.jar5、注意:如果当前项目配置了spring-boot-starter-parent,在这个parent包里面配置了对应的goal配置
只需要简单配置如上即可,如果没有配置spring-boot-starter-parent,那么还需要如下配置:
(If you are using spring-boot-starter-parent, such execution is already pre-configured 
with a repackage execution ID so that only the plugin definition should be added.)
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins>
</build>

2. 两个优点

2.1 依赖管理

<!--父项目做版本管理-->		
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version></parent><!--他的父项目 声明了开发中常用的依赖(jar)的版本号 自动版本仲裁机制-->
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.3.4.RELEASE</version>
</parent>
  • spring-boot starter场景启动器

场景启动器,只要引用了一个场景启动器,这个场景的所有依赖都被引入。

1、spring-boot-starter-* : spring官方提供启动器
2、只要引入starter,这个场景的所有常规需要的依赖我们都自动引入
3、SpringBoot所有场景的支持:
https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems.starters
4、*-spring-boot-starter:第三方提供的启动器
5、所有场景启动器最底层的依赖:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>2.3.4.RELEASE</version><scope>compile</scope></dependency>
  • 无需关注版本号,自动版本仲裁
1、引入依赖默认都可以不写版本
2、引入非版本仲裁的jar(没有在spring-boot-dependencies里面定义)需要申明版本
  • 可以修改版本号
1、查看spring-boot-dependencies里面规定当前依赖的版本用的key(mysql.version)
2、在当前项目里面重写配置
<properties><mysql.version>5.1.43</mysql.version>
</properties>

2.2 自动配置

  • 自动配好Tomcat
    • 引入Tomcat依赖
    • 配置Tomcat
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><version>2.3.4.RELEASE</version><scope>compile</scope></dependency>
  • 自动配好SpringMVC
    • 引入SpringMVC全套组件
    • 自动配好SpringMVC常用组件(功能)
  • 自动配好Web常用功能,如:字符编码问题
    • SpringBoot帮我们配置好了所有web开发的常用场景
  • 默认的包结构
    • 主程序所在包及其所有子包里面的组件都会被默认扫面进来
    • 无需以前的包扫描配置
    • 想要改变扫描路径,@SpringBootApplication(scanBasePackages="com.koral")
      • 或者@ComponentScan指定扫描路径
@SpringBootApplication
等同于
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
  • 各种配置拥有默认值
    • 默认配置最终都是映射到MultipartProperties
    • 配置文件的值最终会绑定到每个类上,这个类会在容器中创建对象
  • 按需加载所有自动配置项
    • 非常多的starter
    • 引入了哪些场景这个场景的自动配置才会开启
    • SpringBoot的所有自动配置功能都在spring-boot-autoconfigure里面
http://www.lryc.cn/news/119705.html

相关文章:

  • 10-1_Qt 5.9 C++开发指南_Data Visualization实现数据三维显示
  • [保研/考研机试] KY87 鸡兔同笼 北京大学复试上机题 C++实现
  • Jmeter快捷方式和应用图标设置
  • PHP sm4国密加密解密文件
  • CBCGPRibbon 添加Edit、Combox、Hyperlink控件
  • 漫话拥塞控制:BBR 是个单流模型
  • HTML详解连载(1)
  • 最新版本2023UI千月影视APP源码 开源完美版前后端完美匹配 后端基于ThinkPHP框架
  • centos7安装Docker详细步骤(无坑版教程)
  • Python入门自学进阶-Web框架——39、redis、rabbitmq、git——2
  • 了解IL汇编跳转语句
  • JVM运行时五大数据区域详解
  • Vuex 使用教程
  • springboot启动you will need to add ‘org.slf4j‘ to prefer-application-packages异常解决
  • 云原生核心原则和特征
  • 【ElasticSearch入门】
  • SQL | 注释
  • oi知识表+NOIP提高组算法及算法思想总结
  • 【mysql】实现递归查询
  • JUC并发编程之原子类
  • 测试设计中隐藏的边界有哪些?
  • 领航优配:暑期旅游市场热度持续攀升,相关公司业绩有望持续释放
  • 基于 CentOS 7 构建 LVS-DR 集群 及 配置nginx负载均衡
  • docker搭建在线Markdown服务器
  • 打靶练习:WestWild 1.1(一个简单但不失优雅的Ubuntu靶机)
  • 【2.3】Java微服务:sentinel服务哨兵
  • 【C++】开源:abseil-cpp基础组件库配置使用
  • 【GPT-3 】创建能写博客的AI工具
  • [保研/考研机试] KY35 最简真分数 北京大学复试上机题 C++实现
  • 算法备案后,企业需要做什么?合规与执行挑战