Springboot(三)配置文件自定義

一、自定義屬性

使用註解

@Value("${屬性名}")

在用IDEA創建一個Spring Boot工程時,系統默認會在src/main/java/resources目錄建一個配置文件application.properties。它也支持yml格式的文件,下面以yml格式的文件來講解如何自定義屬性。

在工程的配置文件application.yml自定義一組屬性,如下:

my:
  name: marvin
  age: 18

如果要讀取配置文件application.yml的屬性值,只需在變量上加@Value("${屬性名}")註解,就可以將配置文件application.yml的一個屬性值賦給一個變量。新建一個Controller,其代碼清單如下:

@RestController
public class MyController {
	@Value("${my.name}")
	private String name;
	@Value("${my.age}")
	private int age;

	@RequestMapping(value = "/getresult")
	public String getResult(){
		return name+":"+age;
	}
}
啓動工程 SpringBoot ,打開瀏覽器訪問“ 句://localhost:8080/getresult ”,瀏覽器顯示如下:
marvin:18
這說明配置文件 application.yml 屬性 my.name my.age 經成功讀入應用程序中。
 
 

二、將配置文件屬性賦值給實體類

使用註解

@ConfigurationProperties(prefix = "配置文件中的數據層級")

@EnableConfigurationProperties({實體類.class})

當有很多配置屬性時,如果逐個地讀取屬性會非常麻煩。通常的做法會把這些屬性名作爲變量名來創建 JavaBean 變量,並將屬性值賦給 JavaBean 變量的值。
 
1、在配置文件 Application.yml 中添加如下屬性
my:
  name: marvin
  age: 18
  number: ${random.int}
  uuid: ${random.uuid}
  max: ${random.int(10)}
  value: ${random.value}
  greeting: hi,i`m ${my.name}

其中,配置文件中用到了 ${random},它可以用來生成各種不同類型的隨機值。
random.in:隨機生成一個 int 類型的值;
random.uuid: 隨機生成 uuid;
random.value: 隨機生成 個值;
random.int(10):隨機生成一個小於10的整數。


2、怎麼將這些屬性賦給 JavaBean 呢?創建一個 JavaBean ,其代碼清單如下:

@ConfigurationProperties(prefix = "my")
@Component
public class ConfigBean {
	private String name;
	private int age;
	private int number;
	private String uuid;
	private int max;
	private String value;
	private String greeting;
	//省略getter setter...
}

在上面的代碼中,在 bean 類添加註解 @ConfigurationProperties,表明該類爲配置屬性類,並加上配置的 prefix ,例如本案例的 "my"。
另外需要在 bean 類加 @Component 註解(Spring Boot 啓動時通過包掃描將該類作爲 Bean 注入 IoC 容器中)
創建 Controller,讀取 ConfigBean 類的屬性。
Controller 類,加 @EnableConfigurationProperties 註解,並指明 bean 類,其代碼清單如下

@RestController
@EnableConfigurationProperties({ConfigBean.class})
public class MyController {

	@Autowired
	private ConfigBean configBean;

	@RequestMapping(value = "/getmarvin")
	public String getResult(){
		return configBean.getGreeting()+"-"+ configBean.getName()+"-"+configBean.getUuid()+"-"+configBean.getMax();
	}
}

 

三、自定義配置文件

使用註解

@Configuration

@PropertySource(value = "classpath:文件名.properties")

@ConfigurationProperties(prefix = "配置文件中的數據層級")

@EnableConfigurationProperties({實體類.class})

上面介紹瞭如何把配置屬性寫到 application.yml 文件中,並把配置屬性讀取到一個配置類中。
有時屬性太多,把所有的配置屬性都寫到 application.yml 配置文件中不太合適。
這時需要自定義配置文件。例如 src/main/resources 目錄下定義 test.properties 文件

com.forezp.name=marvin
com.forezp.age=18

如何將這個配置文件 test.properties 的屬性性和屬性值賦給 JavaBean 呢?
在類名上添加 @Configuration@PropertySource@ConfigurationProperties
需要注意的是,若Spring Boot 1.4 或 1.4 之前,則需要在 @PropertySource 註解上加 location 指明該配置文件的路徑。本案例採用的 Spring Boot 版本爲 1.5 

@Configuration
@PropertySource(value = "classpath:test.properties")
@ConfigurationProperties(prefix = "com.forezp")
public class UserBean {
	private  String name;
	private int age;
	//省略getter setter...
}

寫一個 MyController 在類的上方加上 @RestController 註解,開啓 RestControllerde的功能
加上 @EnableConfigurationProperties 註解,並指明要引用的 JavaBean 開啓引用配置屬性的功能

@RestController
@EnableConfigurationProperties({ConfigBean.class, UserBean.class})
public class MyController {
	@Autowired
	private ConfigBean configBean;
	@RequestMapping(value = "/getmarvin")
	public String getResult(){
		return configBean.getGreeting()+"-"+ configBean.getName()+"-"+configBean.getUuid()+"-"+configBean.getMax();
	}

	@Autowired
	private UserBean userBean;
	@RequestMapping(value = "/getUser")
	public String getUeser(){
		return userBean.getName()+":"+userBean.getAge();
	}
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章