一、自定义属性
当我们创建一个springboot项目的时候,系统默认会为我们在src/main/java/resources目录下创建一个application.properties。
在application.yml自定义一组属性:
persion:
name: 焦学垚
age: 18
sex: 男
love: 篮球,足球,乒乓球
strList: str1,str2,str3
maps: "{'name1':'花花','name2':'小草'}"
如果你需要读取配置文件的值只需要加@Value("${属性名}"):
package com.xiamipangun.hollerword.controller;
import com.xiamipangun.hollerword.entity.Persion;
import com.xiamipangun.hollerword.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/xiami")
public class HolleController {
@Autowired
private Persion persion;
@Value("${love}")
private String[] love;
@Value("#{'${strList}'.split(',')}")
private List<String> strList;
@Value("#{${maps}}")
private Map<String,String> maps;
@Autowired
private Student student;
@RequestMapping("/hello")
@ResponseBody
public String getValue(){
System.out.println(persion);
System.out.println(love.toString());
System.out.println(student);
return "hello word!";
}
}
二、将配置文件的属性赋给实体类
(1) YML文件
persion:
name: 焦学垚
age: 18
sex: 男
package com.xiamipangun.hollerword.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 人
*/
@ConfigurationProperties(prefix = "persion")
@Component
public class Persion {
private String name;
private int age;
private String sex;
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Persion{" +
"name='" + name + '\'' +
", age=" + age +
", sax='" + sex + '\'' +
'}';
}
}
@Controller
@RequestMapping("/xiami")
@EnableConfigurationProperties
public class HolleController {
@Autowired
private Persion persion;
@RequestMapping("/hello")
@ResponseBody
public String getValue(){
System.out.println(persion);
return "hello word!";
}
}
1.需要在JavaBean添加注解@ConfigurationProperties,并加上它的prrfix;添加@Component注解
2.在应用类或者application类,加EnableConfigurationProperties注解。
(2)properties文件
server.port=8111
student.name= 虾米
student.price = 100
student.flag = true
package com.xiamipangun.hollerword.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Configuration
@PropertySource(value= {"classpath:application.properties"},encoding="UTF-8")
@ConfigurationProperties(prefix = "student")
public class Student {
private String name;
private double price;
private boolean flag;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", price=" + price +
", flag=" + flag +
'}';
}
}
需要添加三个注解:
@Configuration
@PropertySource(value = “classpath:test.properties”)
@ConfigurationProperties(prefix = “com.forezp”)