前情提要
在SpringBoot中使用application.properties默認了很多配置,有時候我們也需要到該屬性配置文件中自定義屬性值,甚至於將一系列自定義屬性合併成一個對象,而SpringBoot中也提供了這種功能的實現,那麼現在就來試試吧
使用自定義的屬性並配置成類
首先在applicatiion.properties屬性文件中自定義一系列屬性:
//application.properties配置文件
person.id=1
person.name=chengxi
person.nick=成兮
person.pass=970624
然後創建一個entity實體類,這裏就創建一個person類吧,並使用註解ConfigurationProperties修飾,表示該類通過屬性文件進行配置,代碼如下:
package org.framework.demo.section1;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "person")
public class Person {
private Integer id;
private String name;
private String nick;
private String pass;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
這樣一來,Person類的屬性值在運行時就已經確定值了。如果想要配置的屬性值不是application.properties,而是我們自定義的屬性文件中,直接將ConfigurationProperties註解中的location的值設置成該屬性文件的位置即可。例如我設置application.properties同級下的person.properties文件:
//classpath: person.properties
person.id=1
person.name=chengxi
person.nick=成兮
person.pass=970624
然後在對應的Person類中的註解裏面額外設置一個屬性locations的值:
package org.framework.demo.section1;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "person", locations="classpath:person.properties")
public class Person {
private Integer id;
private String name;
private String nick;
private String pass;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
而SpringBoot1.5版本以後取消了ConfigurationProperties註解的location屬性的設置,所以如果我們想要自定義屬性文件的位置,需要結合Component註解和PropertySource註解的幫助,代碼如下:
package org.framework.demo.section1;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* SpringBoot1.5後的自定義屬性文件的配置測試類
**/
@Component
@ConfigurationProperties(prefix = "person")
@PropertySource("classpath: person.properties")
public class Person {
private Integer id;
private String name;
private String nick;
private String pass;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
最後,我們需要在啓動類中添加Person.class的配置,通過EnableConfigurationProperties註解進行配置,代碼如下:
package org.framework.demo.section1
import org.springframework.boot.Banner
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.web.servlet.ServletComponentScan
import org.springframework.context.annotation.ComponentScan
@SpringBootApplication
//用於配置文件自定義屬性成類
@EnableConfigurationProperties(Person.class)
public class ServletApp2 {
public static void main(String[] args){
//關閉banner的輸出
SpringApplication app = new SpringApplication(ServletApp2.class)
// app.setBannerMode(Banner.Mode.OFF)
app.run(args)
}
}
這樣一來,即可實現與普通的entity實體類一樣的功能了,在後面想要使用Person類的實例直接Autowired自動注入即可,如下:
@Autowired
private Person person;