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);
}
相关推荐
loser.with.m10 小时前
【AgentScope 2.0】05-从数据库配置到可运行 Agent:十个 Builder 开关怎么把一行 JSON 变成 HarnessAgent
人工智能·spring boot·ai
行百里er11 小时前
Spring Insight 里收到的 Span 是怎么存下来的
spring boot·spring·监控
xcl092513 小时前
智慧健身场馆系统开发实战:从架构设计到核心模块落地指南
java·spring boot
vx-程序开发13 小时前
【计算机毕设】基于Spring Boot的古城景区管理系统88564
java·数据库·spring boot·后端·spring·elasticsearch·课程设计
vx-Biye_Design14 小时前
springboot中国传统节日宣传平台49078-计算机课程设计、毕业设计
java·前端·vue.js·spring boot·后端·课程设计·idea
Wang's Blog16 小时前
Java框架快速入门: Spring Security+OAuth2之实现UserDetails与GrantedAuthority深度定制
java·spring·mybatis
代码调试师16 小时前
【毕设分享】springboot攀枝花生鲜电商平台58990
java·vue.js·spring boot·后端·架构·eclipse·课程设计
xcl092516 小时前
全民健身解决方案小程序系统开发实战:从架构设计到上线指南
java·spring boot
洛阳泰山16 小时前
别卷 Python 了:我用 Java 21 + Spring Boot 3 打造了一个企业级 RAG + 智能体工作流引擎(附架构与源码解析)
java·spring boot·后端
树若逝花若殇17 小时前
基于Spring AOP优雅记录请求响应日志
java·spring boot