标签:就是 span sso use pid mic wired alt last
1、yaml基本语法
2、支持的三种数据结构
3、springboot默认有两种配置springboot配置的方式,一种就application.properties,另一种就是application.yml
server:
port: 8080
person:
username: zhangsan
age: 18
email: zhangsan@qq.com
maps: {k1: v1,k2: 12}
lists:
- lis1
- lis2
- lis3
dog:
name: 小黄
age: 2
4、在application.properties同级目录下新建application.yml,在com.gong,springboot下新建bean包,在其下新建Person.java,Dog.java
Person.java
package com.gong.springboot.bean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; //将配置文件中的属性映射到组件中 //prefix:表示配置文件中的哪个下面的属性进行一一映射 @Component @ConfigurationProperties(prefix="person") public class Person { private String username; private Integer age; private String email; private Map<String,Object> maps; private List<Object> lists; private Dog dog; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } 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 Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } @Override public String toString() { return "Person{" + "username=‘" + username + ‘\‘‘ + ", age=" + age + ", email=‘" + email + ‘\‘‘ + ", maps=" + maps + ", lists=" + lists + ", dog=" + dog + ‘}‘; } }
Dog.java
package com.gong.springboot.bean; public class Dog { private String name; private Integer age; 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; } @Override public String toString() { return "Dog{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } }
注意:idea中自动提示getters和setters等方法使用快捷键alt+insert。
同时会看到:
需要将以下加入到pom.xml的dependencies中
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
5、进行测试
在test/java/com.gong,springboot/Myspringboot2ApplicationTests.java进行测试,这里需要注意,创建项目时自动生成的该文件好像不太对,太坑了,换成我们自己的:
package com.gong.springboot; import com.gong.springboot.bean.Person; 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; @RunWith(SpringRunner.class) @SpringBootTest public class Myspringboot2ApplicationTests { @Autowired Person person; @Test public void contextLoads() { System.out.println(person); } }
如果相关的报错,则alt+enter选择导入maven 依赖或者import 相关包即可。我们使用的是org.junit.Test,要在pom.xml中加入
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
最后我们运行该测试方法:在控制台可以看到输出
Person{username=‘zhangsan‘, age=18, email=‘zhangsan@qq.com‘, maps={k1=v1, k2=12}, lists=[lis1, lis2, lis3], dog=Dog{name=‘小黄‘, age=2}}
标签:就是 span sso use pid mic wired alt last
原文地址:https://www.cnblogs.com/xiximayou/p/12246121.html