1. 程式人生 > >Springboot讀取配置檔案、pom檔案及自定義配置檔案

Springboot讀取配置檔案、pom檔案及自定義配置檔案

前言

很多人都知道讀取配置檔案,這是初級做法,上升一點難度是使用java bean的方式讀取自定義配置檔案,但是大家很少有知道讀取pom檔案資訊,接下來我都會講到。

正文

筆者還是基於Spring Boot ::        (v1.5.8.RELEASE),利用的工程有eureka服務和eureka-client客戶端服務。

首先教大家一個最簡單的讀取application.properties檔案內容

下圖是我eureka-client服務全部的配置

eureka.client.service-url.defaultZone=http://admin:
[email protected]
:1111/eureka/ server.port=8762 spring.application.name=eureka-client spring.zipkin.base-url=http://localhost:9411 management.security.enabled=true #啟用shutdown endpoints.shutdown.enabled=true #禁用密碼驗證 endpoints.shutdown.sensitive=true #驗證使用者名稱 security.user.name=admin #驗證密碼 security.user.password=admin #角色 management.security.role=SUPERUSER #指定shutdown endpoint的路徑 endpoints.shutdown.path=/custompath #也可以統一指定所有endpoints的路徑`management.context-path=/manage` #指定管理埠和IP management.port=8081 management.address=127.0.0.1

配置controller,使用 @Value 

 @Value("${management.address}")
 private String address;


    @GetMapping("/ClientService/name")
    public String test(@NotBlank(message = "name 不能為空") @RequestParam("name") String name) {
        if (name.equals("1")) {
            throw new ParameterServiceException("這裡填寫錯誤程式碼,規範應是一個列舉", "描述當前錯誤原因");
        }
        return address;
    }

訪問http://localhost:8762/ClientService/name?name=2

可以看到配置檔案management.address的值已經讀取出來了。

接下來演示讀取pom檔案的資訊,筆者只實驗了讀取部分pom的資訊

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>Edgware.RELEASE</version>
    </parent>
    <groupId>eureka-client</groupId>
    <artifactId>eureka-client</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

 這是我pom檔案中一部分程式碼,現在我要讀取這個專案的版本號,我需要在application.properties檔案加一行程式碼

[email protected]@
@Value("${version}")
private String val;
 


    @GetMapping("/ClientService/name")
    public String test(@NotBlank(message = "name 不能為空") @RequestParam("name") String name) {
        if (name.equals("1")) {
            throw new ParameterServiceException("這裡填寫錯誤程式碼,規範應是一個列舉", "描述當前錯誤原因");
        }
        return val;
    }

訪問http://localhost:8762/ClientService/name?name=2

接下來講解使用java bean的方式讀取自定義配置檔案 

建立一個javaBean::DefineEntity

package com;

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

/**
 * @author yanlin
 * @version v1.3
 * @date 2018-10-22 下午12:38
 * @since v8.0
 **/
@Component
@ConfigurationProperties(prefix = "defineTest")
@PropertySource("classpath:define.properties")
public class DefineEntity {
    private String name;
    private String password;

    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;
    }

    @Override
    public String toString() {
        return "DefineEntity{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

建立一個自定義的配置檔名字叫define.properties,內容如下

defineTest.name=kelly
defineTest.password=helloKelly

配置你的controller

 @Autowired
 DefineEntity defineEntity;
   

    @GetMapping("/ClientService/name")
    public String test(@NotBlank(message = "name 不能為空") @RequestParam("name") String name) {
        if (name.equals("1")) {
            throw new ParameterServiceException("這裡填寫錯誤程式碼,規範應是一個列舉", "描述當前錯誤原因");
        }
        return defineEntity.toString();
    }

啟動,訪問http://localhost:8762/ClientService/name?name=2,結果如下,大功告成啦,是不是so easy!!!

注:對本文有異議或不明白的地方微信探討,wx:15524579896