第2章 Spring Boot 入门案例
2.1 第一个 SpringBoot 项目
2.1.1 步骤
(1) 创建一个 Project,选择类型为 Spring Initializr 快速构建
这里可以设置包名、打包方式等基本信息
(2) 选择 Spring Boot 版本及依赖
会根据选择的依赖自动添加起步依赖并进行自动配置,这里我们只选择SpringWeb
(3) 项目创建完毕
(4) 项目结构
com.example.springbootworkspace包下就是我们的工作目录
SpringbootWorkspaceApplication类就是整个项目的主启动类(入口),执行该程序中的 main 方法,SpringBoot就启动了
static:存放静态资源,如图片、CSS、JavaScript 等
templates:存放 Web 页面的模板文件
application.properties/application.yml: 用于存放程序的各种依赖模块的配置信息,很多集成的配置都可以在该文件中进行配置,例如:端口、数据库连接等
.mvn|mvnw|mvnw.cmd:使用脚本操作执行 maven 相关命令,国内使用较少,可删除
.gitignore:使用版本控制工具 git 的时候,设置一些忽略提交的内容
注意,编写的所有代码都要在入口类的同级目录或下一级目录内
2.1.2启动类(入口)
package com.example.springbootworkspace;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootWorkspaceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootWorkspaceApplication.class, args);
}
}
1、@SpringBootApplication 注解是 Spring Boot 项目的核心注解(唯一),主要作用是开启 Spring 自动配置,如果在 Application 类上去掉该注解,那么不会启动SpringBoot 程序
2、main 方法是一个标准的 Java 程序的 main 方法,主要作用是作为项目启动运行的入口
3、@Controller 及 @ResponseBody 依然是我们之前的 Spring MVC,因为 Spring Boot 的里面依然是使用我们的 Spring MVC + Spring + MyBatis 等框架
2.1.3 pom.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--继承SpringBoot框架的一个父项目,所有自己开发的Spring Boot都必须的继承-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--当前项目的GAV坐标-->
<groupId>com.bjpowernode.springboot</groupId>
<artifactId>springboot-workspace</artifactId>
<version>1.0.0</version>
<!--maven项目名称,可以删除-->
<name>002-springboot-springmvc</name>
<!--maven项目描述,可以删除-->
<description>Demo project for Spring Boot</description>
<!--maven属性配置,可以在其它地方通过${}方式进行引用-->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--SpringBoot框架web项目起步依赖,通过该依赖自动关联其它依赖,不需要我们一个一个去添加了
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringBoot框架的测试起步依赖,例如:junit测试,如果不需要的话可以删除-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<!--SpringBoot提供的打包编译等插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Spring Boot 的父级依赖 spring-boot-starter-parent 配置之后,当前的项目就是 SpringBoot 项目
spring-boot-starter-parent 是一个 Springboot 的父级依赖,开发 SpringBoot 程序都需要继承该父级项目,它用来提供相关的 Maven 默认依赖,使用它之后,常用的 jar包依赖可以省去 version 配置
Spring Boot 提供了哪些默认 jar 包的依赖,可查看该父级依赖的 pom 文件
如果不想使用某个默认的依赖版本,可以通过 pom.xml 文件的属性配置覆盖各个依赖项,比如覆盖 Spring 版本:
<properties>
<spring-framework.version>5.0.0.RELEASE</spring-framework.version >
</properties>
2.2 入门案例
2.2.1 新建SpringBoot项目
由于之前我们已经建好项目了,所以直接拿那个来用即可
2.2.3 创建一个 SpringMVC 的 SpringBoot Controller
具体代码
SpringBootController 类所在包:com.example.springbootworkspace.web
@Controller
@RequestMapping(value = "/index")
public class SpringBootController {
@RequestMapping(value = "/springBoot/say")
public @ResponseBody String say() {
return "Hello,springBoot!";
}
}
注解:
1、@Controller:用于标记在一个类上,被其标记的类标明是一个SpringMVC Controller对象,这个时候分发处理器就会扫描类上的方法是否带有@RequestMapping注解
2、 @RequestMapping(value = “/…”):这个注解用于配置url映射。如果用在类前,表示对于这个类的所有方法的增加前提映射路径,如果用在方法前,就表示单对这个方法的映射路径,它还有一个常用参数method用来表示前后端之间请求的get/post,该参数一般在用在类方法前
3、@ResponseBody表示某个方法返回的结果会被适当的转换器转成指定格式后放在response对象的body区中,通常用来返回JSON数据或者XML
案例的新式写法
@Controller 和@ResponseBody可以结合成@RestController,这样就不需要写两个注解了,该注解放在类前
@RestController
@RequestMapping(value = "/index")
public class SpringBootController {
@RequestMapping(value = "/springBoot/say")
private String say() {
return "Hello,springBoot!";
}
}
2.2.5 运行
通过在控制台的输出,可以看到启动 SpringBoot 框架的过程,会启动一个内嵌的 tomcat,端口号一般为 8080,上下文根为空 。
2.2.6访问
在浏览器中输入 http://localhost:8080/index/springBoot/say 访问
注意路径
2.4 SpringBoot 的核心配置文件
Spring Boot 的核心配置文件用于配置 SpringBoot 程序,名字必须以 application 开始
2.4.1 核心配置格式
application.properties 文件(默认采用该文件)
修改端口号和上下文根
通过修改 application.properties 配置文件,在修改默认 tomcat 端口号及项目上下文件根键值对的 properties 属性文件配置方式
#设置内嵌Tomcat端口号
server.port=9090
#配置项目上下文根
server.servlet.context-path=/start
启动浏览器测试
页面显示结果
因为在配置文件内配置了上下文,所以在url内要在controller要求的路径前面加上在配置文件设置的上下文,同时不要忘了端口号也改了
application.yml 文件
yml 是一种 yaml 格式的配置文件,主要采用一定的空格、换行等格式排版进行配置。
yaml 是一种直观的能够被计算机识别的的数据序列化格式,容易被人类阅读,yaml 类似于 xml,但是语法比 xml 简洁很多,值与前面的冒号配置项必须要有一个空格, yml 后缀也可以使用 yaml 后缀
server:
port: 8080
servlet:
context-path: /springboot
注意:当两种格式配置文件同时存在,使用的是.properties 配置文件
2.4.2 多环境配置
在实际开发的过程中,我们的项目会经历很多的阶段(开发->测试->上线),每个阶段的配置也会不同,例如:端口、上下文根、数据库等,这个时候为了方便在不同的环境之间切换,SpringBoot 提供了多环境配置,相同后缀配置之间的语法并无区别。
此时applicaton.properties是主配置文件
关于properties后缀的配置文件
为每个环境创建一个配置文件,命名必须以 application-环境标识.properties|yml
dev表示开发环境,product表示生产环境,test表示测试环境。
application-dev.properties
#开发环境
#设置内嵌Tomcat默认端口号
server.port=8080
#设置项目的上下文根
server.servlet.context-path=/005-springboot-multi-environment-dev
application-product.properties
#生产环境
#配置内嵌Tomcat默认端口号
server.port=8082
#配置项目上下文根
server.servlet.context-path=/005-springboot-multi-environment-product
application-test.properties
#测试环境
#配置内嵌Tomcat端口号
server.port=8081
#配置项目的上下文根
server.servlet.context-path=/005-springboot-multi-environment-test
在总配置文件 application.properties 进行环境的激活
#SpringBoot的总配置文件
#激活开发环境
#spring.profiles.active=dev
#激活测试环境
#spring.profiles.active=test
#激活生产环境
spring.profiles.active=product
spring.profiles.active的值应与配置文件的环境标识名一致,可以更改总配置文件的配置,重新运行 Application,查看启动的端口及上下文根
同一时间只能active一个,此时选择的是product环境的配置文件
关于yml后缀的配置文件
为每个环境创建一个配置文件,命名必须以 application-环境标识.properties|yml
开发环境配置文件:application-dev.yml
#设置开发环境配置
server: port: 8080 #设置Tomcat内嵌端口号
servlet:
context-path: /dev #设置上下文根
测试环境配置文件:application-test.yml
#设置测试环境配置
server:
port: 9090
servlet:
context-path: /test
生产环境配置文件:application-product.yml
#设置生产环境配置
server:
port: 80
servlet:
context-path: /product
SpringBoot 总配置文件:application.yml
#springboot总配置文件
#激活开发环境
#spring:
# profiles:
# active: dev
#激活测试环境
#spring:
# profiles:
# active: test
#激活生产环境
spring:
profiles:
active: product
2.4.3 Spring Boot 自定义配置属性
在 SpringBoot 的核心配置文件中,除了使用内置的配置项之外,我们还可以在里面自定义属性,然后在类中通过注解去读取配置的属性的值
被注入的属性不一定是Controller内的类,其他业务层也可以
使用@Value 注解
用于逐个读取 application.properties 中的配置
在核心配置文件 applicatin.properties 中,添加两个自定义配置项 school.name 和 website。在 IDEA 中可以看到这两个属性不能被 SpringBoot 识别
application.yml 格式配置文件
#设置端口号及上下文根
server:
port: 9090
servlet:
context-path: /
school:
name: ssm
websit: http://www.baidu.com
在 Controller 类中定义属性,并使用@Value 注解获取自定义配置值,并对其方法进行测试
被Value注解标注的属性就是注入的目标
@Controller
public class SpringBootController {
@Value("${school.name}")
private String schoolName;
@Value("${websit}")
private String websit;
@RequestMapping(value = "/springBoot/say")
public @ResponseBody String say() {
return schoolName + "------" + websit;
}
}
重新运行 Application,在浏览器中进行测试
使用@ConfigurationProperties 注解
该注解将整个文件映射成一个对象,用于自定义配置项比较多的情况案例演示
该注解存在属性 prefix(必),作用可以区分同名配置
Component类
在 com.example.springbootworkspace.model包下:
创建 School 类,并为该类加上 Component 和 ConfigurationProperties 注解
Component注解的作用就是将该类放在Springboot管理的容器当中,当Controller类想要获取实例的时候不需要实例化,直接写一个School引用就可
@Component
@ConfigurationProperties(prefix = "school")
public class School {
private String name;
private String websit;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return websit;
}
public void setWebsite(String websit) {
this.websit = websit;
}
}
创建City类,具体内容跟School类差不多
@Component
@ConfigurationProperties(prefix = "city")
public class City {
private String name;
private String website;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
}
application.properties配置文件
server.port=9090
school.name = "安阳"
school.website = "www.baidu.com"
city.name = "Chongqing"
city.website = "www.baidu.com"
website = "www.baidu.com"
Controller类中注入
@Autowired注解表示为标记的属性 注入在Springboot容器中的 被Component标记的类 的实例
@RestController
public class SpringBootController{
@Autowired
private School school;
@Autowired
private City city;
@RequestMapping(value = "/school")
public Object school(){
return school.getName() + school.getWebsite();
}
@RequestMapping(value = "/city")
public Object city(){
return city.getName() + city.getWebsite();
}
}
测试
警告
在上述操作中我们会碰到一个警告,实际它不会影响程序的执行
按照网上的说法,可以在项目中添加依赖解决这个问题(但是貌似还是会报错,不过实际不会影响程序执行)
<!--解决使用@ConfigurationProperties注解出现警告问题-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
中文乱码
如果在 SpringBoot 核心配置文件中有中文信息,会出现乱码
一般在配置文件中,不建议出现中文(注释除外)
下一篇:
Springboot 之 使用jsp