修改配置文件
spring boot中讀取配置文件除了可以讀取properties後綴的文件外,還引入了yml的讀取,用起來也是相當簡單,我們先把resource下的application.properties修改爲yml文件,接着在其中添加端口號8082。
server:
port: 8082
自定義屬性以及@Value取值
除了我們一般使用yml來定義一些我們需要的屬性外yml還支持自定義屬性,在使用時只需要通過@Value來取值即可。那我們現在yml中添加一些屬性(注意空格)。lw:
name: 老王
age: 27
height: 175cm
weight: 60kg
sex: 男
package com.example.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/test")
@RestController
public class MyController {
@RequestMapping("/hello")
public String hello() {
return "HELLO";
}
@Value("${lw.name}")
private String name;
@Value("${lw.age}")
private Integer age;
@Value("${lw.height}")
private String height;
@Value("${lw.sex}")
private String sex;
@RequestMapping(value = "/lw",method = RequestMethod.GET)
public String getZZPInfo(){
return "lw.name:" + name + " lw.age:" + age + " lw.height:" + height+" lw.aex:"+sex;
}
}
然後我們在剛纔的controller中新寫一個方法去使用@Value取值,我們可以看到我們想要獲取的配置信息已經全部拿到了。
這樣寫如果用很多個字段的時候就需要寫很多valuez註解,我們可以創建一個pojo(實體類)來接收配置文件中的信息,首先我們先創建一個實體類User,給予相一致的屬性,並給予set get方法以及重寫toString方法。
這裏我們使用@Component註解來保證待會該pojo可以被spring容器所注入,使用@ConfigurationProperties(prefix = “lw”)註解來獲取配置文件中前綴爲lw的配置,prefix指的是前綴。當然,在以前的版本中這個註解還有個屬性就是local,這個屬性用來指定文件的位置,現在已經取消,默認的是application.properties,前面我們也提及到了我們是可以另外創建properties文件的,這時候如果我們需要指定的話就需要加上另外一個註解@PropertySource(value = { "文件名" })
package com.example.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix ="lw")
@PropertySource(value = { "文件名" })
public class User {
private String name;
private String height;
private String weight;
private Integer age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User [name=" + name + ", height=" + height + ", weight=" + weight + ", age=" + age + ", sex=" + sex
+ "]";
}
}
之後我們再來寫一個方法來獲取這個實體類,並且將返回類型直接修改爲該實體類,這樣可以直接返回一個實體類出去,更方便了,代碼也更爲整潔
@Autowired
private User user;
@RequestMapping(value = "/user",method = RequestMethod.GET)
public User getUser(){
return user;
}
在這裏我們需要注意幾個地方,第一個就是Controller控制層的包我們應該創建在項目名+Application文件所在包下面,和該文件爲平級目錄,然後在包裏面進行控制層的創建,因爲SpringBoot要求所有的類都是以這個啓動類爲基礎,這樣SpringBoot才能夠對創建的類可視。在這不僅僅是針對控制層Controller而言,包含Dao層Service層以及entity等,也是相同的道理。具體的目錄如下:
這樣就可以返回需要的結果,如下:
但是 如果我們沒有把entity包建得和該文件爲平級目錄,這樣系統就會找不到對相應得bean 也就是實體類,這種情況解決得辦法是在我們對應的controller類上加上@EnableConfigurationProperties({User.class})註解,它的作用是告訴Spring Boot 使能支持@ConfigurationProperties(prefix ="lw")註解,如下: