【日常学习】springboot使用yml注入数据(注入数据错误)
问题:
springboot使用yml注入数据,测试类进行输出,输出的信息与yml文件中含不同(name属性),yml文件中的name属性值为:www,而输出的是shaoz,值得注意的是shaoz是我的windows管理员账户,未解决,以下是代码。。。

输出yml注入数据.PNG
application.yml文件:
name: www
age: 18
birth: 1999/08/14
boss: true
maps: {k1: v1,k2: v2}
lists:
- lisi
- zhaoliu
dog:
dogName: haha
dogAge: 4
User类:(不再写get,set方法)
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 将配置文件中配置的每一个属性值,映射到这个组件中
* @ConfigurationProperties:告诉spring boot将本类中所有的属性和配置文件中相关的配置进行绑定
* prefix这个属性告诉与yml文件的哪个属性进行映射
*/
@Component
@ConfigurationProperties(prefix = "user")
public class User implements Serializable {
private String name;
private Integer age;
private boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
Dog类:
public class Dog {
private String dogName;
private Integer dogAge;</pre>
测试类:
import com.szw.springboot02.springboot02.beans.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
*@SpringBootTest单元测试
*可以在测试期间很方便的类似编码一样进行自动注入等容器一些功能
*@RunWith是一个运行器
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot02ApplicationTests {
@Autowired
public User user;
@Test
public void contextLoads() {
System.out.println(user);
}
}
pom.xml:
<?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.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.szw.springboot02</groupId>
<artifactId>springboot02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot02</name>
<description>Demo project for Spring Boot</description>
<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>
<!--导入配置文件处理器,配置文件绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
网友评论