1.YML配置
program:
age: 12
list:
- 12
- 13
- 14
map:
k1: a
k2: b
string: abc
animal:
name: dog
anInt: 11
2.program类–做输出类
package com.rod.springboot.yaml;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* 功能描述:prefix = "program"是找到yaml文件或者properties里名字为program的属性
*/
@Service
@ConfigurationProperties(prefix = "program")
public class Program {
private String string;
private int age = 0;
private Animal animal;
private List<Integer> list;
private Map<String, String> map;
@Override
public String toString() {
return "Program{" +
"string='" + string + '\'' +
", age=" + age +
", animal=" + animal +
", list=" + list +
", map=" + map +
'}';
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Animal getAnimal() {
return animal;
}
public void setAnimal(Animal animal) {
this.animal = animal;
}
public List<Integer> getList() {
return list;
}
public void setList(List<Integer> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
}
3.springboot-测试类
package com.rod.springboot;
import com.rod.springboot.yaml.Program;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class SpringbootApplicationTests {
@Autowired
private Program program;
@Test
public void contextLoads() {
System.out.println(program);
//Program{string='abc', age=22222, animal=Animal{name='dog', anInt=11}, list=[12, 13, 14], map={k1=a, k2=b}}
}
}
4.pom配置类
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.rod</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
5.总结:
1.springboot自带的test是junit5的,不是4.12的,没有junit5的要去下载jar
2.properties配置比yaml配置要优先,会先识别properties里的数据
3.不能在main里写东西,会有错误,如我–死活输不出来,都是null,但是test没有任何问题
@SpringBootApplication
public class SpringbootApplication {
@Autowired
private Program program;
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
SpringbootApplication springbootApplication = new SpringbootApplication();
System.out.println(springbootApplication.program);
}
}