springboot配置文件及yml的使用
1.配置文件
作用:springboot自动配置是基于约定的,可以使用配置文件对默认的配置或约定进行修改
默认的全局配置文件:
①application.properties :
写法:k=v
示例:
server.port = 8880
②application.yml :yml不是一个标记文档
写法:k:空格v
示例:
server:
port: 8880
path: a\b\c
yml里面默认可以不写引号,“”(双引号)会将其中的转义符转义,其他不转义
xml是一个标记文档:
<server>
<port>8080</port>
<path>a\b\c</path>
</server>
2.yml的使用
①创建一个student类(Student.class)
package com.example.bean;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component //将javabean放入spring容器内
@ConfigurationProperties(prefix = "student")//spring-boot 提供@ConfigurationProperties注解将配置文件的值映射到类上使用
public class Student {
private String name;
private int age;
private boolean sex;
private Date birthday;
private Map<String,Object> location;
private String[] habbies;
private List<String> skills;
private Pet pet;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Map<String, Object> getLocation() {
return location;
}
public void setLocation(Map<String, Object> location) {
this.location = location;
}
public String[] getHabbies() {
return habbies;
}
public void setHabbies(String[] habbies) {
this.habbies = habbies;
}
public List<String> getSkills() {
return skills;
}
public void setSkills(List<String> skills) {
this.skills = skills;
}
public Pet getPet() {
return pet;
}
public void setPet(Pet pet) {
this.pet = pet;
}
public Student(String name, int age, boolean sex, Date birthday, Map<String, Object> location, String[] habbies,
List<String> skills, Pet pet) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.birthday = birthday;
this.location = location;
this.habbies = habbies;
this.skills = skills;
this.pet = pet;
}
public Student() {
super();
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", habbies="
+ Arrays.toString(habbies) + ", skills=" + skills + ", pet=" + pet + "]";
}
}
②创建Pet类(Pet.class)
package com.example.bean;
public class Pet {
private String nickname;
private String strain;
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
@Override
public String toString() {
return "Pet [nickname=" + nickname + ", strain=" + strain + "]";
}
public Pet(String nickname, String strain) {
super();
this.nickname = nickname;
this.strain = strain;
}
public Pet() {
super();
}
}
③编写yml文件(application.yml)
student:
#简单类型
name: djk
age: 20
sex: true
birthday: 2000/07/15
#map类型:
location:
#写法2:
province: sd
city: wf
zone: sg
#写法1:{province: sd,city: wf,zone: sg} 行内写法
#数组类型:
habbies:
[篮球,兵乓球,书法] #行内写法
#- 篮球
#- 兵乓球
#- 书法
#集合类型:
skills:
[计算机,编程,springboot] #行内写法
#- 计算机
#- 编程
#- springboot
#类 类型
Pet:
#写法2
{nickname: xiaobai,strain: jiwawa}
#写法1
#nickname: xiaobai
#strain: jiwawa
④测试
package com.example.SpringbootDemo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.bean.Student;
@RunWith(SpringRunner.class)
@SpringBootTest
@EnableConfigurationProperties(Student.class)//3.通过@Autowired标签即可访问到该对象,不过在使用之前必须在使用类上面增加注解@EnableConfigurationProperties
public class SpringbootDemoApplicationTests {
@Autowired
Student student;
@Test
public void contextLoads() {
System.out.println(student);
}
}
注解
1. @EnableConfigurationProperties(Student.class)
通过@Autowired标签即可访问到该对象,不过在使用之前必须在使用类上面增加注解@EnableConfigurationProperties
2. @ConfigurationProperties(prefix = "student")
spring-boot 提供@ConfigurationProperties注解将配置文件的值映射到类上使用