Itheima-Springboot2【二刷】

1. Springboot parent和starter区别

  • parent :开发Springboot项目需要继承spring-boot-starter-parent,其中定义了若干个依赖管理(坐标版本号 ),避免依赖版本冲突
  • starter :开发Springboot项目需要导入坐标时通常导入对应的starter,每个starter根据功能不同,通常包含多个依赖坐标简化配置

2. 配置文件加载优先级

  • .properties > .yml > .yaml
  • 不同配置文件中相同的配置按照加载优先级相互覆盖,不同配置文件中不同配置全部保留。

3. 配置文件属性提示消失解决方案

4. 读取配置文件属性数据

  • 读取单一属性属性:@Value
java 复制代码
@Value("${server.port}")
private String port;
  • 读取全部属性数据:Environment
java 复制代码
@Autowired
private Environment env; 
...
sout(env.getProperty("server.port"));
  • 自定义对象封装指定数据(目前常用

5. 整合第三方技术

  1. 导入相关依赖(starter)
  2. 配置yml

6. 工程打包与运行

  • mvn clean -> mvn package
  • pom里应包含打包插件spring-boot-maven-plugin
  • java -jar xxxx.jar [(临时属性:)--server.port=8080 --xx=xx] 运行jar文件。

  • linux系统打包运行暂时跳过!

7. 配置文件4级分类


多层级配置文件间的属性采用叠加并覆盖的形式作用于程序。

8. 多环境开发

  • 单文件

  • 多文件(yml) :主文件中设置公共属性,环境分类文件中设置冲突属性

  • 多文件(properties)

  • 多环境分组管理

9. 热部署

  • 手工启动热部署

(1)导入依赖 devtools

java 复制代码
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifcatId>spring-boot-devtools</artifactId>
</dependency>

(2)激活热部署(restart):build project (Ctrl + F9)

  • 自动启动热部署


    激活方式:IDEA失去焦点5s后启动热部署。

10. @ConfigurationProperties 第三方Bean属性绑定

java 复制代码
@Bean
@ConfigurationProperties(prefix="datasource")
public DruidDataSource datasource(){
	DruidDataSource ds = new DruidDataSource();
	return ds;
}
  • 宽松绑定属性名 可以宽松绑定,但绑定前缀名prefix)必须仅能使用纯小写字母、数字、下划线作为合法字符;

  • Bean的属性校验

    (1)导入JSR303和Hibernate校验框架依赖;

    (2)使用@Validated注解启用校验功能;

    (3)使用具体的校验规则规范数据校验格式。

11. web环境模拟测试 webEnvironment =

(1)设置测试端口

java 复制代码
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class WebTest {
	@Test
	void test(){
		xxxxx;
	}
}

(2)模拟测试启动

(3)模拟测试匹配

java 复制代码
@Test
void testGetById(@Autowired MockMvc mvc) throws Exception {
	MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
	ResultActions action = mvc.perform(builder);

	// 测试响应状态
	StatusResultMatchers status =  MockMvcResultMatchers.status();
	ResultMatcher ok = status.isOk();
	action.andExpect(ok);

	// 测试响应头
	StatusResultMatchers header=  MockMvcResultMatchers.header();
	ResultMatcher contentType = header.string("Content_Type", "application/json");
	action.andExpect(contentType);

	// 测试响应体,json
	StatusResultMatchers content=  MockMvcResultMatchers.content();
	ResultMatcher result= content.json("{\"id\":1,\"name\":\"wyw\"}");
	action.andExpect(result);
}
相关推荐
q***697715 小时前
Spring Boot与MyBatis
spring boot·后端·mybatis
r***123816 小时前
SpringBoot最佳实践之 - 使用AOP记录操作日志
java·spring boot·后端
LSL666_17 小时前
SpringBoot自动配置类
java·spring boot·后端·自动配置类
q***783717 小时前
Spring Boot 3.X:Unable to connect to Redis错误记录
spring boot·redis·后端
t***265918 小时前
SpringBoot + vue 管理系统
vue.js·spring boot·后端
qq_124987075319 小时前
基于springboot的疾病预防系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·毕业设计
q***25119 小时前
Spring Boot 集成 Kettle
java·spring boot·后端
百***864621 小时前
Spring Boot应用关闭分析
java·spring boot·后端
tanxiaomi21 小时前
Spring、Spring MVC 和 Spring Boot ,mybatis 相关面试题
java·开发语言·mybatis
程序员西西1 天前
SpringBoot轻松整合Sentinel限流
java·spring boot·后端·计算机·程序员