本人使用的环境是jdk-1.8+idea+maven
使用spring-boot框架
这个是我的maven配置
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--场景启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--配置proerptis提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
先来看看我的实体类的属性:Student.java
//在类上面提供这2个注解
@Component//把实体类加入容器中
@ConfigurationProperties(prefix = “student”)// 告诉SpringBoot将文本的所有属性和配置文件中的相关配置进行绑定; prefix :表示要绑定的前缀,yml用前缀来点出属性赋值
//注意提供get,set方法
private int id;
private String student;
private Map<String, Tcher> map;
private Tcher tcher;
private List tcherList;
第二个实体类:Tcher.java
//在类上面提供这2个注解
@Component//把实体类加入容器中
@ConfigurationProperties(prefix = “tcher”)// 告诉SpringBoot将文本的所有属性和配置文件中的相关配置进行绑定; prefix :表示要绑定的前缀,yml用前缀来点出属性赋值
//注意提供get,set方法
private int anInt;
private String tcherName;
上面是我的2个实体类主要是用来模拟对象集合
这是我的yml文件:注意要放在类路径下比如(resources目录下)
解释:
student:是我的Student对象,对应我@ConfigurationProperties(prefix = “student”)的这个前缀,然后依次给我student对象里面的属性赋值,层级关系是用空格隔开,赋值必须中间有个空格,比如 (属性:空格然后赋值)