一、Spring Boot 入门
Spring Boot 来简化Spring应用开发的一个框架,约定大于配置
Spring Boot 的底层用的就是Spring
访问官网:spring.io 再点击projects
1.背景
问题
J2EE笨重的开发,繁多的配置、低下的开发效率、复杂的部署流程,第三发技术集成难度大。
解决
“Spring全家桶”时代
Spring Boot -> J2EE一站式解决方案
Spring Cloud ->分布式整体解决方案
优点
快速创建独立运行的Spring项目以及与主流框架集成
使用嵌套式的Servlet容器,应用无需打成WAR包
starters自动依赖与版本控制
大量的自动配置简化开发,也可修改默认值
无需配置XML,无代码生成,开箱即用
准成产环境的运行时应用监控
与云计算的天然集成
微服务简介
martinfowler.com
微服务:架构分格
一个应用应该是一组小型服务;可以通过http的方式进行互通
2.环境准备
jdk1.8
maven3.x
intellijIDEA
SpringBoot
MAVEN设置:
给maven的setting.xml配置文件的profiles标签添加
(F:\jsp\apache-maven-3.6.1-bin\apache-maven-3.6.1\conf)
jdk-1.8
<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>
2.IDEA的默认修改: 修改IDEA的默认maven ,具体看下面的博客https://blog.csdn.net/weixin_43034040/article/details/103835125 HelloWorld项目 浏览器发送hello请求,服务器接收请求并处理,响应Hello World 字符串 第1步.创建一个maven工程(jar) create new project ->maven->next 第2步.导入spring boot相关依赖 来到spring boot 官网
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
第3步 .编写一个主程序:启动Spring Boot
package com.atguigu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//Spring应用启动起来
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
第4步.编写相关的Controller、Service
package com.atguigu.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello world";
}
}
如果很多发那个发都是RESTAPI的方式,即发送数据给浏览器,而不是页面跳转,则
@ResponseBody可以放在类外
@ResponseBody
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "Hello world";
}
}
@ResponseBody
@Controller
两个合并:@RestController
//@ResponseBody
//@Controller
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "Hello world";
}
}
第5步.运行并访问
运行主程序
浏览器输入:http://localhost:8080/hello
第6步.简化部署
就算没有装tomcat环境也可以运行
a.导入插件:
11. 开发你的第一个 Spring Boot Application
www.docs4dev.com
将以下代码添加到pom中
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
b.打包
点击Idea右边的maven ->Lifecycle->package
此时maven介入进行打包,打包的地址为
Building jar:
G:\java\spring-boot-01-helloworld\target\spring-boot-01-helloworld-1.0-SNAPSHOT.jar
jar包内容:
BOOT-INF\classes:自己写的类
BOOT-INF\lib:maven依赖导入的,包含了tomcat
c.命令行运行
在jar包所在目录下,命令行运行:java -jar spring-boot-01-helloworld-1.0-SNAPSHOT.jar
d.浏览器运行
浏览器输入:http://localhost:8080/hello
3.HelloWorld项目解析
POM文件
父项目
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
查看spring-boot-starter-parent,它也有有父项目
org.springframework.boot
spring-boot-dependencies
1.5.9.RELEASE
…/…/spring-boot-dependencies
查看spring-boot-dependencies,有定义了每一个依赖的版本,真正管理Spring Boot应用里的所有应用版本
<activemq.version>5.14.5</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.59</appengine-sdk.version>
<artemis.version>1.5.5</artemis.version>
…
…
…
导入starters启动器
导入
org.springframework.boot
spring-boot-starter-web
spring-boot-starter:spring-boot场景启动器
spring-boot-starter-web:帮我们导入web模块正常运行所依赖的组件。
starters启动器
参考官网:
Spring Boot Reference Guide
docs.spring.io
Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里引入这些starter相关的所有依赖都会导入进来。要用什么就导入什么场景启动器。
主程序类,入口类
/**
* @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//Spring应用启动起来,run(