1、编写SpringBoot的引导类

package springboot_test.springboot_test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication
public class SpringbootTestApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootTestApplication.class, args);
} }

2、编写普通实体Dog

package springboot_test.springboot_test.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; //普通实体,会做为Person类中的属性使用,不需要加任何注解
//@ConfigurationProperties(prefix = "person.dog")
//@Component
public class Dog {
private String name;
private String color; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
} @Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
'}';
}
}

3、编写需要绑定yml文件中的值的配置类Person
package springboot_test.springboot_test.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.stereotype.Component; import java.util.Date;
import java.util.List;
import java.util.Map; //prefix指定使用yml配置文件中的哪部分开头的数据
@ConfigurationProperties(prefix = "person")
//该类也做为组件才能使用容器中的其他组件,即才能使用@ConfigurationProperties
@Component
public class Person {
private String name;
private String age;
private Date birthDay; private Map map;
private List list;
//类中含有其他类的对象,想让dog在yml编写的时候也给提示,需要加上该属性
@NestedConfigurationProperty
private Dog dog; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public Date getBirthDay() {
return birthDay;
} public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
} public Map getMap() {
return map;
} public void setMap(Map map) {
this.map = map;
} public List getList() {
return list;
} public void setList(List list) {
this.list = list;
} public Dog getDog() {
return dog;
} public void setDog(Dog dog) {
this.dog = dog;
} @Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", birthDay=" + birthDay +
", map=" + map +
", list=" + list +
", dog=" + dog +
'}';
}
} 4、配置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>
<!--依赖的父项目,父项目中还会依赖其他父项目来决定使用的jar包的版本-->
<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>springboot_test</groupId>
<artifactId>springboot_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_test</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties>
<!--用于引进springmvc等相关jar包,可通过页面访问-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--用于junit测试-->
<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-devtools</artifactId>
</dependency> <!--配置处理器,当类上加@ConfigurationProperties注解时,需要配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies> <!--用于使用maven的package功能进行打包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project> 5、编写application.yml文件
person:
name: zhangsan
age: 18
birth-day: 2019/11/19
map: {k1: v1,k2: v2}
list:
- pig
- cat
dog:
color: black
name: 小狗 6、编写PersonTest.java测试类
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;
import springboot_test.springboot_test.SpringbootTestApplication;
import springboot_test.springboot_test.bean.Person; @SpringBootTest(classes = SpringbootTestApplication.class)
@RunWith(SpringRunner.class)
public class PersonTest {
@Autowired
private Person person; @Test
public void getPerson(){
System.out.println(person);
}
} 另外在测试的时候出现了报错java.lang.Exception: No runnable methods,原因为@Test注解引入的类不对,应该引入org.junit.Test;
还有一个可能的原因是没有在方法上写@Test注解 另外,如果绑定的值是在application.properties中配置的,而不是在yml中配置的,则会出现打印结果中文乱码的问题,application.properties配置如下:
person.name=张三
person.age=18
person.birth-day= 2019/11/19
person.list=a,b,c
person.map.k1=v1
person.map.k2=v2
person.dog.name=小狗
person.dog.color=black
可通过如下所示
另外为了通过配置文件给某个类绑定某个属性值,不应该通过application.properties去绑定,而应该单独配置一个properties,
但是单独配置的properties,SpringBoot是无法识别的,需要在绑定的类Person.java上加上如下注解:
@PropertySource(value = "classpath:person.properties"),即
1、resources下新建person.properties文件内容如下:
person.name=张三
person.age=18
person.birth-day= 2019/11/19
person.list=a,b,c
person.map.k1=v1
person.map.k2=v2
person.dog.name=小黄
person.dog.color=black 2、Person.java改为如下:
package springboot_test.springboot_test.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; import java.util.Date;
import java.util.List;
import java.util.Map; //prefix指定使用yml配置文件中的哪部分开头的数据
@ConfigurationProperties(prefix = "person")
//该类也做为组件才能使用容器中的其他组件,即才能使用@ConfigurationProperties
@Component
//作用是使其可以与person.properties文件中的内容,但是,若application.properties中也存在可绑定的内容,
// 则以application.properties中的内容为准,若不存在,才会以person.properties中的内容为准
@PropertySource(value = "classpath:person.properties")
public class Person {
private String name;
private String age;
private Date birthDay; private Map map;
private List list;
//类中含有其他类的对象,想让dog在yml编写的时候也给提示,需要加上该属性
@NestedConfigurationProperty
private Dog dog; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public Date getBirthDay() {
return birthDay;
} public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
} public Map getMap() {
return map;
} public void setMap(Map map) {
this.map = map;
} public List getList() {
return list;
} public void setList(List list) {
this.list = list;
} public Dog getDog() {
return dog;
} public void setDog(Dog dog) {
this.dog = dog;
} @Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", birthDay=" + birthDay +
", map=" + map +
", list=" + list +
", dog=" + dog +
'}';
}
}

如有理解不到位的地方,望指教!
 

												

0005SpringBoot中用Junit测试实体类中绑定yml中的值的更多相关文章

  1. 实体类在Windows程序中的高级应用--------------------》》心境是一种境界。

    一.事务 我们在大家学到这,或多或少对事务都有一些理解了.今天的我也对事务有了更深一层的理解对我来说,我想与大家一起分享一下. 解析: 1.ADO.NET提供了事务处理功能 2.C#中开启事务 3.在 ...

  2. WPF——传实体类及绑定实体类属性

    public class User: private string _User; public string User1 { get { return _User; } set { _User = v ...

  3. 泛型实体类List<>绑定到repeater

    后台代码: private void bindnewslist() { long num = 100L; List<Model.news> news = _news.GetList(out ...

  4. 关于mybatis对实体类参数绑定参数的问题

    dao层的代码: public interface SupplierMapper extends BaseMapper<SupplierDbo>{ /*List<SupplierDb ...

  5. java中遍历实体类属性和类型,属性值

    public static void testReflect(Object model) throws NoSuchMethodException, IllegalAccessException, I ...

  6. list<实体类>互相嵌套和前台取值问题

    list<实体类>嵌套list<实体类>,必须保证嵌套的实体类里面有这个list对象,把这个list<实体类>当做一个对象 这是需要解析的数据,并把这些数据封装成l ...

  7. Mybatis实体类的映射文件中select,insert语句使用

    id:在命名空间中唯一的标识符,可以被用来引用这条语句. parameterType:设置传入这条语句的参数的数据类型,如int,String...... resultType:设置从这条语句中返回数 ...

  8. 实体类调用泛型父类中的静态方法中执行CRUD——第二版

    using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; namespa ...

  9. C#获取一个实体类的属性名称、属性值

    using System.Reflection; Type t = obj.GetType();//获得该类的Type foreach (PropertyInfo pi in t.GetPropert ...

随机推荐

  1. c#,回文数判断

    回文数:将数值反过来.如:123 反过来是321 ,如果两个数相等,则是回文,否则不是 using System; namespace ConsoleApp1 { class Program { st ...

  2. 认识 Spring 框架(一)

    认识 Spring 框架 Spring 框架是 Java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 IoC (Inversion of Control,控制反转) 和 AOP( ...

  3. mosquitto安装遇到问题和解决办法

    问题1 make编译报错,提示xsltproc命令未找到 解决办法: yum  install libxslt 问题2 make编译报错,提示: failed to load external ent ...

  4. [NOIP普及组2011]装箱问题

    目录 链接 博客链接 题目链接 题目内容 题目描述 格式 输入 输出 样例 输入 输出 前缀知识 题解 题目名称:装箱问题 来源:2011年NOIP普及组 链接 博客链接 CSDN 洛谷博客 题目链接 ...

  5. T100——错误信息提示传入参数显示

    LET l_str1 = l_xccc.xccc901LET l_str2 = l_inat015LET l_str = l_str1.trim(),'|',l_str2.trim() CALL cl ...

  6. 使用QSaveFile类安全的读写文件(继承自QFileDevice,与QFile并列)

    QSaveFile类也是一种I/O设备,来用来读写文本文件和二进制文件,但使用该类的话,在写入操作失败时不会导致已经存在的数据丢失. 该类在执行写操作时,会先将内容写入到一个临时文件中,如果没有错误发 ...

  7. (三)easyUI之树形组件

    一.同步树 1.1 概念 所有节点一次性加载完成 1.2 案例 1.2.1 数据库设计 1.2.2 编码 index.jsp <%@ page language="java" ...

  8. Node在Sublime Text3下环境搭建(node02)

    一.下载sublime Text的nodejs插件 https://github.com/tanepiper/SublimeText-Nodejs 二.下载后解压 直接改名为nodejs 放到 Pre ...

  9. 利用Supervisor 管理自己部署的应用程序

    首先,在centos7下安装supervisor yum install python-setuptools easy_install supervisor 然后新建配置文件 #新建superviso ...

  10. JDBC 学习复习9 配置Tomcat数据源

    在实际开发中,我们有时候还会使用服务器提供给我们的数据库连接池,比如我们希望Tomcat服务器在启动的时候可以帮我们创建一个数据库连接池,那么我们在应用程序中就不需要手动去创建数据库连接池,直接使用T ...