瞭解過spring-Boot這個技術的,應該知道Spring-Boot的核心配置文件application.properties,當然也可以通過註解自定義配置文件的信息。
Spring-Boot讀取配置文件的方式:
一.讀取核心配置文件信息application.properties的內容
核心配置文件是指在resources根目錄下的application.properties或application.yml配置文件,讀取這兩個配置文件的方法有兩種,都比較簡單。
核心配置文件application.properties內容如下:
- test.msg=Hello World SpringBoot
方式一:使用@Value方式(常用)
- <span style="font-size:18px;">package Solin.controller;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class WebController {
- @Value("${test.msg}")
- private String msg;
- @RequestMapping("/index1")
- public String index1(){
- return "方式一:"+msg;
- }
- }</span>
訪問:http://localhost:8088/index1時得到:"方式一:Hello World SpringBoot"
方式二:使用Environment方式
- <span style="font-size:18px;">package Solin.controller;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.core.env.Environment;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class WebController {
- @Autowired
- private Environment env;
- @RequestMapping("/index2")
- public String index2(){
- return "方式二:"+env.getProperty("test.msg");
- }
- }
- </span>
訪問:http://localhost:8088/index2時得到:"方式二:Hello World SpringBoot"
二.讀取自定義配置文件信息,例如:author.properties
爲了不破壞核心文件的原生態,但又需要有自定義的配置信息存在,一般情況下會選擇自定義配置文件來放這些自定義信息,這裏在resources
目錄下創建配置文件author.properties
resources/author.properties
內容如下:
- author.name=Solin
- author.age=22
創建管理配置的實體類:
- package Solin.controller;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.stereotype.Component;
- //加上註釋@Component,可以直接其他地方使用@Autowired來創建其實例對象
- @Component
- @ConfigurationProperties(prefix = "author",locations = "classpath:author.properties")
- public class MyWebConfig{
- private String name;
- private int age;
- 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;
- }
- }
注意:
在@ConfigurationProperties註釋中有兩個屬性:
locations:指定配置文件的所在位置
prefix:指定配置文件中鍵名稱的前綴(我這裏配置文件中所有鍵名都是以author.開頭)
使用@Component是讓該類能夠在其他地方被依賴使用,即使用@Autowired註釋來創建實例。
創建測試Controller
- package Solin.controller;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- @Controller
- public class ConfigController {
- @Autowired
- private MyWebConfig conf;
- @RequestMapping("/test")
- public @ResponseBody String test() {
- return "Name:"+conf.getName()+"---"+"Age:"+conf.getAge();
- }
- }
注意:由於在Conf類上加了註釋@Component,所以可以直接在這裏使用@Autowired來創建其實例對象。
訪問:http://localhost:8088/test時得到:"Name:Solin---Age:22"