1、SpringBoot配置
1.1配置文件类型
application.yml
server:
port: 80
#person:
# name: lxj
# age: 22
# sex: true
# maps: {k1: v1,k2: 12}
# lists:
# - hahaha
# - hehehe
# animal:
# name: 蝙蝠
# age: 10
application.properties
server.port=80
1.2yml基本语法
1)字面量:普通的值
name: lxj
“”:双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思
2)k: v:在下一行来写对象的属性和值的关系;注意缩进
person:
name: lxj
age: 22
3)数组(List、Set):
用- 值表示数组中的一个元素
lists:
- hahaha
- hehehe
行内写法
pets: [cat,dog,pig]
1.3读取配置文件
application.yml
server:
port: 80
person:
name: lxj
age: 22
sex: true
maps: {k1: v1,k2: 12}
lists:
- hahaha
- hehehe
animal:
name: 蝙蝠
age: 10
Person.java
@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value = {"classpath:person.properties"})
public class Person {
// @Value("${person.name}")
private String name;
private Integer age;
private Boolean sex;
private Date birth;
private Map<String, Object> maps;
private List<Object> lists;
private Animal animal;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getSex() {
return sex;
}
public void setSex(Boolean sex) {
this.sex = sex;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Animal getAnimal() {
return animal;
}
public void setAnimal(Animal animal) {
this.animal = animal;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", animal=" + animal +
'}';
}
}
1.4Profile多环境支持
application.yml
spring:
profiles:
active: test
---
server:
port: 80
spring:
profiles:dev
---
server:
port: 8081
spring:
profiles:test
---
server:
port: 8082
spring:
profiles:fac
2、SpringBoot测试
步骤
2.1导入spring-boot-starter-test
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
2.2创建一个SpringBoot应用
@SpringBootApplication
public class App {
//启动入口
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
2.3写SpringBoot测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class SpringTest {
@Autowired
private TestService testService;
@Test
public void test() {
testService.Test();
}
}