SpringBoot-Properties
SpringBoot Demo 獲取配置檔案屬性
一、專案目錄結構
二、配置檔案
1、主配置檔案:application.yml
server:
port: 8080 #埠
servlet:
context-path: /properties #訪問路徑
spring:
profiles:
active: dashu #配置環境
2、環境配置檔案:application-dashu.yml
#配置環境引數
pom:
name: @artifactId@
version: @version@
packaging: @packaging@
user:
name: dashu
website: https://blog.csdn.net/weixin_43521890?spm=1000.2115.3001.5113
email: dashuplus@163.con
3、額外的spring配置後設資料:additional-spring-configuration-metadata.json
{
"properties": [
{
"name": "pom.name",
"description": "Default value is name in pom.xml.",
"type": "java.lang.String"
},
{
"name": "pom.version",
"description": "Default value is version in pom.xml.",
"type": "java.lang.String"
},
{
"name": "pom.packaging",
"description": "Default value is packaging in pom.xml.",
"type": "java.lang.String"
}
]
}
4、maven依賴檔案: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.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dashu</groupId>
<artifactId>properties</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>properties</name>
<packaging>jar</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<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>
<scope>test</scope>
</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>
<!--Lombok能以簡單的註解形式來簡化java程式碼,提高開發人員的開發效率。-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--Hutool是一個小而全的Java工具類庫,
通過靜態方法封裝,降低相關API的學習成本,
提高工作效率,使Java擁有函式式語言般的優雅,
讓Java語言也可以“甜甜的”。-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、專案檔案
1、啟動類:PropertiesApplication.java
package com.dashu.properties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* <p>
* SpringBoot啟動類
* </p>
*
* @package: com.dashu.properties
* @description: SpringBoot啟動類
* @author: dashu
* @date: Created in 2020/11/19
* @copyright: Copyright (c)
* @version: V1.0
* @modified: dashu
*/
@SpringBootApplication
@Slf4j
public class PropertiesApplication {
public static void main(String[] args) throws UnknownHostException {
ConfigurableApplicationContext application = SpringApplication.run(PropertiesApplication.class, args);
Environment env = application.getEnvironment();
//獲取本機ip
String ip = InetAddress.getLocalHost().getHostAddress();
//獲取配置檔案埠
String port = env.getProperty("server.port");
//獲取配置檔案專案啟動路徑
String path = env.getProperty("server.servlet.context-path");
//日誌輸出訪問路徑
log.info("\n----------------------------------------------------------\n\t" +
"Application helloword is running! Access URLs:\n\t" +
"URL: \t\thttp://"+ip+":" + port + path + "\n\t" +
"----------------------------------------------------------");
}
}
2、配置實體類
(1)YmlProperty.java
package com.dashu.properties.entity;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* <p>
* yml主配置屬性實體類
* </p>
*
* @package: com.dashu.properties.entity
* @description: 主配置屬性
* @author: dashu
* @date: Created in 2020/11/19
* @copyright: Copyright (c)
* @version: V1.0
* @modified: dashu
*/
@Data
@Component
public class YmlProperty {
@Value("${user.name}")
private String name;
@Value("${user.website}")
private String website;
@Value("${user.email}")
private String email;
}
(2)PomProperty.java
package com.dashu.properties.entity;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* <p>
* pom.xml依賴屬性實體類
* </p>
*
* @package: com.dashu.properties.entity
* @description: 依賴屬性
* @author: dashu
* @date: Created in 2020/11/19
* @copyright: Copyright (c)
* @version: V1.0
* @modified: dashu
*/
@Data
@Component
@ConfigurationProperties(prefix = "pom")
public class PomProperty {
private String name;
private String version;
private String packaging;
}
3、訪問控制類:PropertyController.java
package com.dashu.properties.controller;
import cn.hutool.core.lang.Dict;
import com.dashu.properties.entity.PomProperty;
import com.dashu.properties.entity.YmlProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 訪問控制類
* </p>
*
* @package: com.dashu.properties.controller
* @description: 訪問控制類
* @author: dashu
* @date: Created in 2020/11/19
* @copyright: Copyright (c)
* @version: V1.0
* @modified: dashu
*/
@RestController
@Slf4j
public class PropertyController {
private final PomProperty pomProperty;
private final YmlProperty ymlProperty;
/**
* 構造方法注入元件
* @param pomProperty
* @param ymlProperty
*/
@Autowired
public PropertyController(PomProperty pomProperty, YmlProperty ymlProperty) {
this.pomProperty = pomProperty;
this.ymlProperty = ymlProperty;
}
@GetMapping("getProperty")
public Dict property(){
log.info("pom:"+ pomProperty);
log.info("yml:"+ ymlProperty);
return Dict.create().set("pom", pomProperty).set("yml", ymlProperty);
}
}
四、執行
1、專案啟動
2、訪問介面
3、返回資料格式化
五、詩詞欣賞
蜀道難
[唐] 李白
噫籲嚱,危乎高哉!蜀道之難,難於上青天!
蠶叢及魚鳧,開國何茫然!
爾來四萬八千歲,不與秦塞通人煙。
西當太白有鳥道,可以橫絕峨眉巔。
地崩山摧壯士死,然後天梯石棧相鉤連。
上有六龍回日之高標,下有衝波逆折之回川。
黃鶴之飛尚不得過,猿猱欲度愁攀援。
青泥何盤盤,百步九折縈巖巒。
捫參歷井仰脅息,以手撫膺坐長嘆。
問君西遊何時還?畏途巉巖不可攀。
但見悲鳥號古木,雄飛雌從繞林間。
又聞子規啼夜月,愁空山。
蜀道之難,難於上青天,使人聽此凋朱顏!
連峰去天不盈尺,枯鬆倒掛倚絕壁。
飛湍瀑流爭喧豗,砯崖轉石萬壑雷。
其險也如此,嗟爾遠道之人胡為乎來哉!
劍閣崢嶸而崔嵬,一夫當關,萬夫莫開。
所守或匪親,化為狼與豺。
朝避猛虎,夕避長蛇;磨牙吮血,殺人如麻。
錦城雖雲樂,不如早還家。
蜀道之難,難於上青天,側身西望長諮嗟!