Spring boot項目學習系列一——獲取自定義配置,以及多環境配置文件的切換

最近只要稍微學一點東西就覺得很有收穫,好像自己的儲備徹徹底底不夠用了,需要趕緊補充點技術能量

git上搜到一個大神項目https://github.com/xkcoding/spring-boot-demo,感覺超級棒,於是開啓spring boot項目學習之旅

 

目標:獲取配置文件的自定義配置,以及多個環境之間如何切換配置文件

理解:不是很到位沒有理解目標的含義,看到這篇文章https://segmentfault.com/a/1190000020247777之後突然理解了

 additional-spring-configuration-metadata.json該文件的內容是元數據,我自己理解成它是對在配置文件中配置時會用的鍵值或者說屬性的定義,然後在配置文件application.xml或者是application.properties中會有提示,這裏我做了個小實驗,後面會講到

一、目錄:

二、元數據: additional-spring-configuration-metadata.json,它的位置是在src/main/resources/META-INF文件夾下面,

META-INF文件夾是自己建的。

{
  "properties": [
    {
      "name": "application.name",
      "description": "Default value is artifactId in pom.xml.",
      "type": "java.lang.String"
    },
    {
      "name": "application.version",
      "description": "Default value is version in pom.xml.",
      "type": "java.lang.String"
    },
    {
      "name": "developer.name",
      "description": "The Developer Name.",
      "type": "java.lang.String"
    },
    {
      "name": "developer.website",
      "description": "The Developer Website.",
      "type": "java.lang.String"
    },
    {
      "name": "developer.qq",
      "description": "The Developer QQ Number.",
      "type": "java.lang.String"
    },
    {
      "name": "developer.phone-number",
      "description": "The Developer Phone Number.",
      "type": "java.lang.String"
    }
  ]
}

三、主配置文件application.yml

server:
  port: 8080
  servlet:
    context-path: /demo
spring:
  profiles:
    active: dev

四、各環境獨有的配置

1)dev環境:application-dev.xml

application:
  name: dev環境 @artifactId@
  version: dev環境 @version@
developer:
  name: dev環境 xkcoding
  website: dev環境 http://xkcoding.com
  qq: dev環境 237497819
  phone-number: dev環境 17326075631

2)prod環境:application-prod.xml

application:
  name: prod環境 @artifactId@
  version: prod環境 @version@
developer:
  name: prod環境 xkcoding
  website: prod環境 http://xkcoding.com
  qq: prod環境 237497819
  phone-number: prod環境 17326075631


五、pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-boot-properties-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-properties-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--
		在 META-INF/additional-spring-configuration-metadata.json 中配置
		可以去除 application.yml 中自定義配置的紅線警告,並且爲自定義配置添加 hint 提醒
		 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.5.0</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

六、code

1)ApplicationProperty.java

package com.example.springbootpropertiesdemo;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 項目配置
 */

@Data
@Component
public class ApplicationProperty {

    @Value("${application.name}")
    private String name;
    @Value("${application.version}")
    private String version;
}

2)DeveloperProperty.java

package com.example.springbootpropertiesdemo;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 開發人員配置信息
 */

@Data
@ConfigurationProperties(prefix = "developer")
@Component
public class DeveloperProperty {
    private String name;
    private String website;
    private String qq;
    private String phoneNumber;

}

3)PropertyController.java

package com.example.springbootpropertiesdemo;

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

import java.util.HashMap;
import java.util.Map;

@RestController
public class PropertyController {

    private final ApplicationProperty applicationProperty;
    private final DeveloperProperty developerProperty;

    @Autowired
    public PropertyController(ApplicationProperty applicationProperty, DeveloperProperty developerProperty) {
        this.applicationProperty = applicationProperty;
        this.developerProperty = developerProperty;
    }

    @GetMapping("properties")
    public Map<String ,Object> index(){
        Map<String,Object> map = new HashMap<>();
        map.put("applicationProperty",applicationProperty);
        map.put("developerProperty",developerProperty);
        return  map;
    }
}

七、訪問

 

八、提示小實驗——不同後綴配置文件中分別輸入自定義配置鍵的首字母

1).yml

2).properties

總結一下:即使有元數據文件,yml配置文件還是沒有提示,但是properties文件有提示,所以是開發人員用properties更改合適嗎,哈哈

 

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