springboot 配置文件讀取的兩種方式,以及使用到的注解解釋


了解過spring-Boot這個技術的,應該知道Spring-Boot的核心配置文件application.properties,當然也可以通過注解自定義配置文件的信息。

pom文件

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
    <!-- 單元測試使用 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot</artifactId>
      <version>1.5.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
      <version>1.5.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
      <version>1.5.6.RELEASE</version>
    </dependency>
  </dependencies>

 

Spring-Boot讀取配置文件的方式:

一.讀取核心配置文件信息application.properties的內容

     核心配置文件是指在resources根目錄下的application.properties或application.yml配置文件,讀取這兩個配置文件的方法有兩種,都比較簡單。

核心配置文件application.properties內容如下:

test.msg=Hello World SpringBoot
test.name=test
test.password= 123test

方式一:使用@Value方式(常用)

package cn.ar.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Jayden
 * Date: 2018/6/28
 * Time: 9:57
 */
@RestController
//@PropertiesSource("classpath:application.properties")
public class FirstController { @Value("${test.name}") private String name; @Value("${test.password}") private String password; @RequestMapping(value = "/test") public String say(){ return name+":application.propertoes里面的name值"+"/t"+password+"密碼"; } }

注意:1.在@Value的${}中包含的是核心配置文件中的鍵名。在Controller類上加@RestController表示將此類中的所有視圖都以JSON方式顯示,類似於在視圖方法上加@ResponseBody。

2.@PropertySource配置文件路徑設置,在類上添加注解,如果在默認路徑下可以不添加該注解 ,我這里就在默認路徑下,所以上面沒用這個注解。
如果有多及目錄, 比如 classpath:config/my.properties指的是src/main/resources目錄下config目錄下的my.properties文件.

3.多配置文件引用,若取兩個配置文件中有相同屬性名的值,則取值為最后一個配置文件中的值:

比如:@PropertySource({"classpath:config/my.properties","classpath:config/config.properties"})

訪問:http://localhost:8080/test時得到:"方式一:test:application.propertoes里面的name值/t123test密碼"

 

方式二:使用Environment方式

package cn.ar.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;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Jayden
 * Date: 2018/6/28
 * Time: 9:57
 */
@RestController
public class FirstController {
   /* @Value("${test.name}")
    private String  name;
    @Value("${test.password}")
    private String password;

    @RequestMapping(value = "/test")
    public String  say(){
        return name+":application.propertoes里面的name值"+"/t"+password+"密碼";
    }*/
   @Autowired
   private Environment env;
    @RequestMapping(value = "/test")
    public String  say(){
        return env.getProperty("test.name") + "      " +env.getProperty("test.password");
    }


}

注意:這種方式是依賴注入Evnironment來完成,在創建的成員變量private Environment env上加上@Autowired注解即可完成依賴注入,然后使用env.getProperty("鍵名")即可讀取出對應的值。

 

 

二.讀取自定義配置文件信息,例如:detifalManager.properties

  1. 首先建立對象與配置文件映射關系
  2. 方法中使用自動注入方式,將對象注入,調用get方法獲取屬性值
  3. 注意:新版本的@ConfigurationProperties沒有了location屬性,使用@PropertySource來指定配置文件位置
  4. prefix=”user1”指的是配置文件中的前綴,如user1.name,在定義對象屬性名時為private String name;
  5. 讀取配置文件中的集合時,使用List來接收數據,但List必須先實例化,負責會報錯

為了不破壞核心文件的原生態,但又需要有自定義的配置信息存在,一般情況下會選擇自定義配置文件來放這些自定義信息,這里在resources目錄下創建配置文件detifalManager.properties

resources/detifalManager.properties內容如下:

user1.name=admin11111111
user1.password=admin123qwqwq
user1.age= 15
user1.sex=男

 

創建管理配置的實體類:

package cn.ar.controller;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * User: Jayden
 * Date: 2018/6/28
 * Time: 10:24
 */
@Component//加上注釋@Component,可以直接在其他地方使用@Autowired來創建其實列對象
@ConfigurationProperties(prefix = "user1")//設置配置文件的前綴
@PropertySource("classpath:detifalManager.properties")//設置自定義文件的路徑
public class Manager {
    private String name;
    private String password;
    private String sex;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

注意:
    在@ConfigurationProperties注釋中有兩個屬性:
新版的springboot 沒有這個屬性:locations:指定配置文件的所在位置,可以使用@propertySource("classpath:****")指定
prefix:指定配置文件中鍵名稱的前綴(我這里配置文件中所有鍵名都是以user1.開頭)
    使用@Component是讓該類能夠在其他地方被依賴使用,即使用@Autowired注釋來創建實例。

創建測試Controller

package cn.ar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * User: Jayden
 * Date: 2018/6/28
 * Time: 10:33
 */
@RestController
public class SecondController {
    @Autowired
    Manager manager;
    @RequestMapping("/test1")
    public String say(){
        return  "name"+ manager.getName() + "password" + manager.getPassword();
    }
}

注意:由於在manager類上加了注釋@Component,所以可以直接在這里使用@Autowired來創建其實例對象。

訪問:http://localhost:8080/test1時得到:"name admin11111111 password  admin123qwqwq"

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM