src/main/resources/application.yml
person:
name: zhangsan
sex: famale
age: 19
isMerried: false
books: [三国演义,红楼梦,水浒传]
pets:
- 金毛
- 多不多啦
- 吉娃娃
friends:
[{name: zhangxueyou,sex: male},{name: 刘德华,sex: 男}]
src/main/java/com/study/springboot02/bean/Friend.java
import org.springframework.stereotype.Component;
@Component
public class Friend {
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Friend{" +
"name='" + name + '\'' +
", sex=" + sex +
'}';
}
}
src/main/java/com/study/springboot02/bean/Person.java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private String sex;
private Integer age;
private boolean isMerried;
private List<String> books;
private String [] pets;
private List<Friend> friends;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public boolean isMerried() {
return isMerried;
}
public void setMerried(boolean merried) {
isMerried = merried;
}
public List<String> getBooks() {
return books;
}
public void setBooks(List<String> books) {
this.books = books;
}
public String[] getPets() {
return pets;
}
public void setPets(String[] pets) {
this.pets = pets;
}
public List<Friend> getFriends() {
return friends;
}
public void setFriends(List<Friend> friends) {
this.friends = friends;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", isMerried=" + isMerried +
", books=" + books +
", pets=" + Arrays.toString(pets) +
", friends=" + friends +
'}';
}
}
src/test/java/com/study/springboot02/Springboot02ApplicationTests.java
@SpringBootTest
class Springboot02ApplicationTests {
@Autowired
private Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
src/main/java/com/study/springboot02/bean/Friend.java
import org.springframework.stereotype.Component;
@Component
public class Friend {
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Friend{" +
"name='" + name + '\'' +
", sex=" + sex +
'}';
}
}
src/main/java/com/study/springboot02/bean/Person.java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Component
@ConfigurationProperties(prefix = "person1")
public class Person {
private String name;
private String sex;
private Integer age;
private boolean isMerried;
private List<String> books;
private String [] pets;
private List<Friend> friends;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public boolean isMerried() {
return isMerried;
}
public void setMerried(boolean merried) {
isMerried = merried;
}
public List<String> getBooks() {
return books;
}
public void setBooks(List<String> books) {
this.books = books;
}
public String[] getPets() {
return pets;
}
public void setPets(String[] pets) {
this.pets = pets;
}
public List<Friend> getFriends() {
return friends;
}
public void setFriends(List<Friend> friends) {
this.friends = friends;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", isMerried=" + isMerried +
", books=" + books +
", pets=" + Arrays.toString(pets) +
", friends=" + friends +
'}';
}
}
server.port=9999
person1.name=lisi
person1.sex=famale
person1.age=19
person1.isMerried=false
person1.books[0]=三国演义
person1.books[1]=红楼梦
person1.pets[0]=金子
person1.pets[1]=银子
person1.pets[2]=童子
person1.friends[0].name=zhangsan
person1.friends[0].sex=male
person1.friends[1].name=lisi
person1.friends[1].sex=female
src/test/java/com/study/springboot02/Springboot02ApplicationTests.java
@SpringBootTest
class Springboot02ApplicationTests {
@Autowired
private Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
application.properties和application.yml同时都有的情况下,
优先读取application.properties的配置内容
未制定配置文件,默认application.properties和application.yml配置文件
需要在Idea中配置编码为utf-8
如果设置完后,还是显示乱码,需要在properties中添加一句:
spring.http.encoding.enabled=true
src/main/java/com/study/springboot02/bean/Person.java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Component
//@ConfigurationProperties(prefix = "person")
public class Person {
@Value("${person.name}")
private String name;
private String sex;
private Integer age;
private boolean isMerried;
private List<String> books;
private String [] pets;
private List<Friend> friends;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public boolean isMerried() {
return isMerried;
}
public void setMerried(boolean merried) {
isMerried = merried;
}
public List<String> getBooks() {
return books;
}
public void setBooks(List<String> books) {
this.books = books;
}
public String[] getPets() {
return pets;
}
public void setPets(String[] pets) {
this.pets = pets;
}
public List<Friend> getFriends() {
return friends;
}
public void setFriends(List<Friend> friends) {
this.friends = friends;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", isMerried=" + isMerried +
", books=" + books +
", pets=" + Arrays.toString(pets) +
", friends=" + friends +
'}';
}
}
src/test/java/com/study/springboot02/Springboot02ApplicationTests.java
@SpringBootTest
class Springboot02ApplicationTests {
@Autowired
private Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
1、自定义一个properties文件,并且让key和之前的properties不一样;
2、使用propertysource注解 来标明你要读取的properties文件名;
3、跟之前一样的读取方式,注意修改prefix的值;
src/main/resources/person2.properties
person.name=lisi2
person.sex=famale2
person.age=12
person.isMerried=false
person.books[0]=三国演义2
src/main/java/com/study/springboot02/bean/Person.java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Component
@ConfigurationProperties(prefix = "person2")
public class Person {
private String name;
private String sex;
private Integer age;
private boolean isMerried;
private List<String> books;
private String [] pets;
private List<Friend> friends;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public boolean isMerried() {
return isMerried;
}
public void setMerried(boolean merried) {
isMerried = merried;
}
public List<String> getBooks() {
return books;
}
public void setBooks(List<String> books) {
this.books = books;
}
public String[] getPets() {
return pets;
}
public void setPets(String[] pets) {
this.pets = pets;
}
public List<Friend> getFriends() {
return friends;
}
public void setFriends(List<Friend> friends) {
this.friends = friends;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", isMerried=" + isMerried +
", books=" + books +
", pets=" + Arrays.toString(pets) +
", friends=" + friends +
'}';
}
}
src/test/java/com/study/springboot02/Springboot02ApplicationTests.java
@SpringBootTest
class Springboot02ApplicationTests {
@Autowired
private Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}