基础回顾
约定优于配置
框架应该提供合理的默认值
两大特性
Spring 的优点
- IOC
- AOP
Spring 的缺点
- 配置是重量级的
- 依赖管理耗时耗力
解决方案(两大特性)
- 起步依赖:解决 jar 包版本问题
- 自动配置:将配置 bean 注册进 IOC 容器
案例实现
使用 Spring Initializr (start.spring.io) 创建项目
引入 spring-boot-starter-web 依赖
单元测试与热部署
单元测试
引入 spring-boot-starter-test 依赖
package com.chen4393.demo.controller;
@RestController // @Controller + @ResponseBody
public class DemoController {
@RequestMapping("/demo")
public String demo() {
return "你好, Spring Boot!";
}
}
package com.chen4393.demo;
@SpringBootTest // 标记为单元测试类,加载项目的ApplicationContext上下文环境
class DemoApplicationTests {
@Autowired
private DemoController demoController;
@Test
void contextLoads() {
String demo = demoController.demo();
Assertions.assertEquals("你好, Spring Boot!", demo);
}
}
热部署
- 引入 spring-boot-devtools 依赖
- IDEA 中配置:
- 【File】-【Settings】-【Compiler】,勾选【build project automatically】
- 【shift】+【Alt】+【Ctrl】+【/】打开【Registry】菜单,勾选【compiler.automake.allow.when.app.runing】
全局配置文件
Properties 配置文件
application.properties
person.id=1
person.name=tom
person.hobby=吃饭,睡觉,看书
person.family=father,mother
person.map.k1=v1
person.map.k2=v2
person.pet.type=dog
person.pet.name=旺财
Yaml 配置文件
扩展名为 .yaml 或 .yml
application.yaml
person:
id: 1
hobby: [吃饭,睡觉,看书]
name: tom
family: [father, mother]
map: {k1: v1, k2: v2}
pet: {type: dog, name: 旺财}
application.properties 配置文件会覆盖 application.yaml 配置文件
利用配置文件注入属性值
使用 @ConfigurationProperties 注入属性值
@Component
@ConfigurationProperties(prefix = "person") // 将配置文件中以person开头的属性赋值
public class Person {
private int id;
private String name;
private List<String> hobby;
private String[] family;
private Map<String, Object> map;
private Pet pet;
// getters and setters(required)
// toString()
}
使用 @Value 注入属性值
@Component
public class Student {
@Value("3")
private int id;
@Value("${person.name}")
private String name;
}
自定义配置类
使用 @PropertySource 加载自定义配置文件
test.properties
test.id=101
test.name=chen4393
MyProperties.java
@Component
@PropertySource("classpath:test.properties")
@ConfigurationProperties(prefix = "test")
public class MyProperties {
private int id;
private String name;
// getters and setters
// toString()
}
package com.chen4393.demo;
@SpringBootTest // 标记为单元测试类,加载项目的ApplicationContext上下文环境
class DemoApplicationTests {
@Autowired
private MyProperties myProperties;
@Test
void testMyProperties() {
System.out.println(myProperties);
}
}
使用 @Configuration 编写自定义配置类
package com.chen4393.demo.config;
public class MyService {
}
package com.chen4393.demo.config;
@Configuration
public class MyConfig {
@Bean("iService")
public MyService myService() {
return new MyService();
}
}
package com.chen4393.demo;
@SpringBootTest // 标记为单元测试类,加载项目的ApplicationContext上下文环境
class DemoApplicationTests {
@Autowired
private ApplicationContext applicationContext;
@Test
void testIOC() {
Assertions.assertTrue(applicationContext.containsBean("iService"));
}
}
随机数设置及参数间引用
随机数设置
test.properties
test.id=${random.int[1,100]}
test.name=chen4393
参数间引用
application.properties
tom.age=${random.int[10,20]}
tom.description=tom的年龄可能是${tom.age}
package com.chen4393.demo;
@SpringBootTest // 标记为单元测试类,加载项目的ApplicationContext上下文环境
class DemoApplicationTests {
@Value("${tom.description}")
private String description;
@Test
void placeHolderTest() {
System.out.println(description);
}
}