SpringBoot入门
SpringBoot项目启动时,控制台输出哪些内容?
- 输出的第一行是JDK的路径
- 还输出了项目启动的端口号,JDK版本、tomcat版本
- 最后一行是Application启动成功的日志
第一个Demo项目构建
- 参照官网:https://spring.io/quickstart
也可以通过idea插件Spring initializr生成项目
注意:SpringBoot 注解 @SpringBootApplication(启动类注解) 默认扫描当前类的同包以及子包下的类。
不符合这个要求可能发生404,请求不到。
如图结构:如果demo下有个controller是访问不到的。
- 使用Junit,SpringBootTest进行单元测试。
Junit的使用(Junit 5版本)
@BeforeEach 在每个测试方法之前执行。注解在【非静态方法】上。
@AfterEach 在每个测试方法之后执行。注解在【非静态方法】上。
@BeforeAll 在当前类中的所有测试方法之前执行。注解在【静态方法】上。
@AfterAll 在当前类中的所有测试方法之后执行。注解在【静态方法】上。
@Test 加到测试方法上,标识测试方法。
@RunWith 加到测试类上,指定通过什么运行。如@RunWith(SpringRunner.class),
在Spring容器下运行。
SpringBootTest的使用(常用注解)
@SpringBootTest 加载测试类上,是SpringBoot的一个用于测试的注解,通过 SpringApplication在测试中创建ApplicationContext。
通过指明启动类,可以解决Unable to find a @SpringBootConfiguration,
you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test的异常问题。
@AutoConfigureMockMvc 是用于自动配置MockMvc,MockMvc可以实现模拟Http请求。
Demo如下:
package com.example.demo;
import com.flash.DemoApplication;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
@AutoConfigureMockMvc
class DemoApplicationTests {
// 自动注入mockMvc对象
@Autowired
private MockMvc mvc;
@BeforeEach
void before(){
System.out.println("===================测试开始==================");
}
@AfterEach
void After(){
System.out.println("===================测试结束==================");
}
@Test
void test01() {
System.out.println("===================测试中1==================");
}
@Test
void test02() {
System.out.println("===================测试中2==================");
}
@Test
void test03() throws Exception {
MvcResult result = mvc.perform(MockMvcRequestBuilders.get("/hello")
.param("name","鲁智深"))
.andReturn();
System.out.println(result.getResponse().getContentAsString());
}
}
总结:
- Junit可以单元测试,比如测试service业务层,测试mapper持久层都可以。
- 如果要测试完整的后台rest api服务,需要使用MockMvc去发起http请求。
配置文件-application.properties
- 配置数据库连接、日志相关配置
# mysql数据库连接参数
spring.datasource.url=jdbc:mysql://localhost:3306/dev
spring.datasource.username=root
spring.datasource.password=123456
# 可省略 mysql8 (com.mysql.cj.jdbc.Driver)
spring.datasource.driverClassName=com.mysql.jdbc.Driver
server.port=8080
- 自定义配置\读取
配置
# 自定义测试属性
com.flash.test.name=鲁智深
com.flash.test.action=倒拔垂杨柳
# 参数替换
com.flash.test.desc=${com.flash.test.name}正在${com.flash.test.action}
读取,通过@Value注解
@Value("${com.flash.test.name}")
private String testName;
@Value("${com.flash.test.action}")
private String testAction;
@Value("${com.flash.test.desc}")
private String testDesc;
- 多环境多配置文件
配置文件满足application-{profile}.properties的格式,其中{profile}对应环境标识
比如:
application-dev.properties:开发环境
application-test.properties:测试环境
application-prod.properties:生产环境
在application.properties文件中通过spring.profiles.active属性设置对应{profile}值,控制哪个具体的
配置文件会被加载。如spring.profiles.active=test就会加载application-test.properties配置文件内容
- yml
除了.properties文件外,SpringBoot也支持.yml格式的配置文件。
示例:
spring:
profiles:
active: prod
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test
username: root
password: root
其他
java -jar xxx.jar --server.port=8888,通过使用–server.port属性来设置xxx.jar应用的端口为8888。
等价于在application.properties中添加属性server.port=8888
禁用命令,提高系统安全性:SpringApplication.setAddCommandLineProperties(false)