最近只要稍微学一点东西就觉得很有收获,好像自己的储备彻彻底底不够用了,需要赶紧补充点技术能量
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更改合适吗,哈哈