在SpringBoot,可以定義一個全局配置文件,全局配置文件有兩種形式:
1、 application.properties
2、application.yml
二者的後綴名不同,編輯的格式也不同,但都是全局配置文件,二者選其一即可,都可以起到相同的作用
在你的maven工程下的src/main/resources 新建一個文件,以 application.properties爲例。
然後編輯以下內容如下:
這裏配置了 server:
port: 8082
等等訪問地址爲:http://localhost:8082/hello
然後在src/main/java下新建一個book.class並寫入以下的代碼
package test1;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@EnableAutoConfiguration //自動配置 ?
@Controller
public class book {
@Value("${book.name}")
private String name;
@Value("${book.author}")
private String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@RequestMapping("hello")
@ResponseBody
public String helloTest(){
System.out.println(name+""+author);
return"hello"+" "+name+" "+author;
}
public static void main(String[] args) {
SpringApplication.run(book.class, args);
}
}
保存後構建maven,然後運行該book.class
就可以在相應的地址(http://localhost:8082/hello)獲取對應的屬性。
運行結果
如果application.properties中的屬性比較少,用上述的方式是可行的,但是如果application.properties中有很多的屬性的時候,每次都要寫一次@Value比較麻煩,
於是有 @ConfigurationProperties實現類型安全的配置(在application.properties的基礎上)。
如下圖:@ConfigurationProperties(prefix = “book”),寫在獲取成員變量的類前。
application.properties配置文件的內容和上面的相同,具體的實現如下:
package test1;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@EnableAutoConfiguration //自動配置 ?
@Controller
@ConfigurationProperties(prefix = "book")
public class book {
//@Value("${book.name}")
private String name;
//@Value("${book.author}")
private String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@RequestMapping("hello-info")
@ResponseBody
public String helloTest(){
System.out.println(name+""+author);
return"helloInfo"+" "+name+" "+author;
}
public static void main(String[] args) {
SpringApplication.run(book.class, args);
}
}
運行結果:
對於類中的成員變量命名,要是application.properties屬性中除了前綴外的剩餘部分,比如我的application.properties中的 book.name,那麼想獲取其屬性時,就要定義一個String name,此外,成員變量需要get和set方法。
最後,關於application.properties配置文件中,屬性的命名最好都已相同的前綴開頭(比如上面都是以book開頭)
這樣,就不用每次都寫@Value了