赞
踩
- <mirrors>
- <mirror>
- <id>nexus-aliyun</id>
- <mirrorOf>central</mirrorOf>
- <name>Nexus aliyun</name>
- <url>http://maven.aliyun.com/nexus/content/groups/public</url>
- </mirror>
- </mirrors>
-
- <profiles>
- <profile>
- <id>jdk-1.8</id>
- <activation>
- <activeByDefault>true</activeByDefault>
- <jdk>1.8</jdk>
- </activation>
- <properties>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
- </properties>
- </profile>
- </profiles>

需求:浏览发送/hello请求,响应 Hello,Spring Boot 2
在第一次引入的时候会很慢,需要耐心等待:
- <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>
- package com.lsy.boot;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- /**
- * @Description:主程序类
- * @Author:lsy
- * @Date:
- */
-
- //@SpringBootApplication:表示这是一个Springboot的应用
- @SpringBootApplication
- public class MainApplication {
- public static void main(String[] args) {
- SpringApplication.run(MainApplication.class,args);
- }
- }

- package com.lsy.boot.controller;
-
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * @Description:
- *
- * @RestController 中就包含了ResponseBody和Controller
- *
- * @Author:lsy
- * @Date:
- */
-
-
- @RestController
- public class HelloController {
-
-
- @GetMapping("/hello")
- public String handle01(){
- return "Hello,SpringBoot 2!";
- }
- }

直接运行主程序中的main方法:
可以看到控制台的信息:
表示运行成功
可以在application.properties中设置一些相关的配置:在SpringBoot的官方文档中可以找到
例如可以修改服务器的端口号:在application.properties写入以下语句就可以了
server.port=8888
父项目做依赖管理:
- 依赖管理
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.3.4.RELEASE</version>
- </parent>
-
- 他的父项目
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>2.3.4.RELEASE</version>
- </parent>
-
- 几乎声明了所有开发中常用的依赖的版本号,自动版本仲裁机制
在开发时导入starter场景启动器。就会自动导入依赖包:
- 1、见到很多 spring-boot-starter-* : *就某种场景,也就是某种引用所需要的的场景启动器
-
- 2、只要引入starter,这个场景的所有常规需要的依赖我们都自动引入
- 3、SpringBoot所有支持的场景
- https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter
- 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,要写版本号。
可以修改默认的版本号:
- 1、查看spring-boot-dependencies里面规定当前依赖的版本 用的 key。
- 2、在当前项目里面重写配置
- <properties>
- <mysql.version>5.1.43</mysql.version>
- </properties>
自动配好Tomcat
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- <version>2.3.4.RELEASE</version>
- <scope>compile</scope>
- </dependency>
自动配好SpringMVC
自动配好Web常见功能,如:字符编码问题
SpringBoot帮我们配置好了所有web开发的常见场景
默认的包结构
- @SpringBootApplication
- 等同于
- @SpringBootConfiguration
- @EnableAutoConfiguration
- @ComponentScan("com.atguigu.boot")
各种配置拥有默认值
非常多的starter
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。