本文是看黑马老师讲课视频学习笔记整理
目录
入门案例
基于IDEA联网
java
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping
public String getById()
{
System.out.println("springboot is running");
return "springboot is running";
}
}
下面来看一个错误示例(其实就是第一次创搞错了):

这种结构导致接口路径未正确映射。要确保接口类位于控制器类和启动类(@SpringBootApplication注解)的同包或子包下,以确保自动解析

这样才正确
然后运行Days3ApplicationTests


SpringBoot程序要基于jdk8进行使用
Spring程序与SpringBoot程序对比

基于 idea 开发 SpringBoot 程序需要确保联网且能够加载到程序框架结构
基于Springboot官网创建

点击CTRL+回车选项下载


java
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping
public String getById()
{
System.out.println("springboot is running");
return "springboot is running...2";
}
}

基于阿里云创建项目


同理
java
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping
public String getById()
{
System.out.println("springboot is running3");
return "springboot is running...3";
}
}

手工创建

Days_05是普通maven工程
这时候把Days3的pom.xml文件中的<parent>和<dependency>复制过去

java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}


隐藏文件

不想看到的文件点+号输入文件格式即可
入门案例解析:
parent


starter
SpringBoot 中常见项目名称,定义了当前项目使用的所有依赖坐标,以达到 减少依赖配置 的目的
比如原来写了7/8个依赖,现在写一个就行。

实际开发:
1.使用任意坐标时,仅书写 GAV 中的 G 和 A , V 由 SpringBoot 提供,除非 SpringBoot 未提供对应版本 V
2. 如发生坐标错误,再指定 Version (要小心版本冲突)
引导类

java
@SpringBootApplication
public class Days3Application {
public static void main(String[] args) {
ConfigurableApplicationContext context= SpringApplication.run(Days3Application.class, args);
BookController bean=context.getBean(BookController.class);
System.out.println("bean======>"+bean);
User user=context.getBean(User.class);
System.out.println(user);
}
}
java
import org.springframework.stereotype.Component;
@Component
public class User {
}
其实SpringApplication.run(Days3Application.class, args);是启动了一个Spring容器,我们定义的bean可以加载进去.
- SpringBoot 的引导类是 Boot 工程的执行入口,运行 main 方法就可以启动项目
- SpringBoot 工程运行后初始化 Spring 容器,扫描引导类所在包加载 bean
内嵌tomcat
使用 maven 依赖管理变更起步依赖项
XML
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--web起步依赖环境中,排除Tomcat起步依赖-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加Jetty起步依赖,版本由SpringBoot的starter控制-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>
内置服务器
- tomcat( 默认) apache 出品,粉丝多, 应用面广 ,负载了若干较重的组件
- undertow undertow ,负载性能 勉强 跑赢 tomcat
- jetty 更轻量级 ,负载性能远不及 tomcat
-
内嵌 Tomcat 服务器是 SpringBoot 辅助功能之一
2. 内嵌 Tomcat 工作原理是将 Tomcat 服务器作为对象运行,并
将该对象交给 Spring 容器管理
3. 变更内嵌服务器思想是去除现有服务器,添加全新的服务
器