@@ -1,191 +1,107 @@ | |||||
# spring-boot-demo-properties | |||||
## spring-boot-demo-properties | |||||
> 本 demo 演示如何获取配置文件的自定义配置,以及如何多环境下的配置文件信息的获取 | > 本 demo 演示如何获取配置文件的自定义配置,以及如何多环境下的配置文件信息的获取 | ||||
## pom.xml | |||||
### 1.开发步骤 | |||||
#### 1.1.添加依赖 | |||||
```xml | ```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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |||||
<modelVersion>4.0.0</modelVersion> | |||||
<artifactId>spring-boot-demo-properties</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
<packaging>jar</packaging> | |||||
<name>spring-boot-demo-properties</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<parent> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>spring-boot-demo</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
</parent> | |||||
<properties> | |||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |||||
<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.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-test</artifactId> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.projectlombok</groupId> | |||||
<artifactId>lombok</artifactId> | |||||
<optional>true</optional> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>cn.hutool</groupId> | |||||
<artifactId>hutool-all</artifactId> | |||||
</dependency> | |||||
</dependencies> | |||||
<build> | |||||
<finalName>spring-boot-demo-properties</finalName> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-maven-plugin</artifactId> | |||||
</plugin> | |||||
</plugins> | |||||
</build> | |||||
</project> | |||||
<!-- | |||||
在 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> | |||||
``` | ``` | ||||
## ApplicationProperty.java | |||||
#### 1.2.配置类 | |||||
> 分别使用两种方式获取配置 | |||||
```java | ```java | ||||
/** | |||||
* <p> | |||||
* 项目配置 | |||||
* </p> | |||||
* | |||||
* @author yangkai.shen | |||||
* @date Created in 2018-09-29 10:50 | |||||
*/ | |||||
@Data | @Data | ||||
@Component | @Component | ||||
public class ApplicationProperty { | public class ApplicationProperty { | ||||
@Value("${application.name}") | |||||
private String name; | |||||
@Value("${application.version}") | |||||
private String version; | |||||
@Value("${application.name}") | |||||
private String name; | |||||
@Value("${application.version}") | |||||
private String version; | |||||
} | } | ||||
``` | |||||
## DeveloperProperty.java | |||||
```java | |||||
/** | |||||
* <p> | |||||
* 开发人员配置信息 | |||||
* </p> | |||||
* | |||||
* @author yangkai.shen | |||||
* @date Created in 2018-09-29 10:51 | |||||
*/ | |||||
@Data | @Data | ||||
@ConfigurationProperties(prefix = "developer") | @ConfigurationProperties(prefix = "developer") | ||||
@Component | @Component | ||||
public class DeveloperProperty { | public class DeveloperProperty { | ||||
private String name; | |||||
private String website; | |||||
private String qq; | |||||
private String phoneNumber; | |||||
private String name; | |||||
private String website; | |||||
private String email; | |||||
} | } | ||||
``` | ``` | ||||
## PropertyController.java | |||||
#### 1.3.测试入口 | |||||
```java | ```java | ||||
/** | |||||
* <p> | |||||
* 测试Controller | |||||
* </p> | |||||
* | |||||
* @author yangkai.shen | |||||
* @date Created in 2018-09-29 10:49 | |||||
*/ | |||||
@RestController | @RestController | ||||
public class PropertyController { | 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("/property") | |||||
public Dict index() { | |||||
return Dict.create().set("applicationProperty", applicationProperty).set("developerProperty", developerProperty); | |||||
} | |||||
private final ApplicationProperty applicationProperty; | |||||
private final DeveloperProperty developerProperty; | |||||
@Autowired | |||||
public PropertyController(ApplicationProperty applicationProperty, DeveloperProperty developerProperty) { | |||||
this.applicationProperty = applicationProperty; | |||||
this.developerProperty = developerProperty; | |||||
} | |||||
@GetMapping("/property") | |||||
public Dict index() { | |||||
return Dict.create().set("applicationProperty", applicationProperty).set("developerProperty", developerProperty); | |||||
} | |||||
} | } | ||||
``` | ``` | ||||
## additional-spring-configuration-metadata.json | |||||
> 位置: src/main/resources/META-INF/additional-spring-configuration-metadata.json | |||||
```json | |||||
{ | |||||
"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" | |||||
} | |||||
] | |||||
} | |||||
#### 1.4.配置文件 | |||||
**application.yml** | |||||
```yml | |||||
server: | |||||
port: 8080 | |||||
servlet: | |||||
context-path: /demo | |||||
spring: | |||||
profiles: | |||||
active: prod | |||||
``` | |||||
**application-dev.yml** | |||||
```yml | |||||
application: | |||||
name: dev环境 @artifactId@ | |||||
version: dev环境 @version@ | |||||
developer: | |||||
name: dev环境 xkcoding | |||||
website: dev环境 https://xkcoding.com | |||||
email: dev环境 237497819@qq.com | |||||
``` | ``` | ||||
**application-prod.yml** | |||||
```yml | |||||
application: | |||||
name: prod环境 @artifactId@ | |||||
version: prod环境 @version@ | |||||
developer: | |||||
name: prod环境 xkcoding | |||||
website: prod环境 https://xkcoding.com | |||||
email: prod环境 237497819@qq.com | |||||
``` | |||||
#### 1.5. 其他 | |||||
编写配置提示,`additional-spring-configuration-metadata.json` | |||||
参考: src/main/resources/META-INF/additional-spring-configuration-metadata.json | |||||
### 2.测试 | |||||
1. 启动 `PropertiesApplication.java` ; | |||||
2. 打开任意浏览器,输入 `http://localhost:8080/demo/property` ,检查输出结果; | |||||
3. 将 `application.yml` 配置文件中的 `spring.profiles.active` 参数从 `prod` 修改为 `dev` ,重新启动,观察输出结果变化。 |
@@ -1,75 +1,74 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | <?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" | <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"> | 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> | |||||
<modelVersion>4.0.0</modelVersion> | |||||
<artifactId>demo-properties</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
<packaging>jar</packaging> | |||||
<artifactId>demo-properties</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
<packaging>jar</packaging> | |||||
<name>demo-properties</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<name>demo-properties</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<parent> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>spring-boot-demo</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
</parent> | |||||
<parent> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>spring-boot-demo</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
</parent> | |||||
<properties> | |||||
<java.version>17</java.version> | |||||
</properties> | |||||
<properties> | |||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |||||
<java.version>1.8</java.version> | |||||
</properties> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>common-tools</artifactId> | |||||
</dependency> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-web</artifactId> | |||||
</dependency> | |||||
<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.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-test</artifactId> | |||||
<scope>test</scope> | |||||
</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> | |||||
<dependency> | |||||
<groupId>org.projectlombok</groupId> | |||||
<artifactId>lombok</artifactId> | |||||
<optional>true</optional> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.projectlombok</groupId> | |||||
<artifactId>lombok</artifactId> | |||||
<optional>true</optional> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>cn.hutool</groupId> | |||||
<artifactId>hutool-all</artifactId> | |||||
</dependency> | |||||
</dependencies> | |||||
</dependencies> | |||||
<build> | |||||
<finalName>demo-properties</finalName> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-maven-plugin</artifactId> | |||||
</plugin> | |||||
</plugins> | |||||
<resources> | |||||
<resource> | |||||
<directory>src/main/resources</directory> | |||||
<filtering>true</filtering> | |||||
<build> | |||||
<finalName>demo-properties</finalName> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-maven-plugin</artifactId> | |||||
</plugin> | |||||
</plugins> | |||||
<resources> | |||||
<resource> | |||||
<directory>src/main/resources</directory> | |||||
<filtering>true</filtering> | |||||
</resource> | </resource> | ||||
</resources> | </resources> | ||||
</build> | |||||
</build> | |||||
</project> | </project> |
@@ -9,12 +9,12 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; | |||||
* </p> | * </p> | ||||
* | * | ||||
* @author yangkai.shen | * @author yangkai.shen | ||||
* @date Created in 2018-09-29 10:48 | |||||
* @date Created in 2022-08-12 21:12 | |||||
*/ | */ | ||||
@SpringBootApplication | @SpringBootApplication | ||||
public class SpringBootDemoPropertiesApplication { | |||||
public class PropertiesApplication { | |||||
public static void main(String[] args) { | public static void main(String[] args) { | ||||
SpringApplication.run(SpringBootDemoPropertiesApplication.class, args); | |||||
SpringApplication.run(PropertiesApplication.class, args); | |||||
} | } | ||||
} | } |
@@ -1,4 +1,4 @@ | |||||
package com.xkcoding.properties.property; | |||||
package com.xkcoding.properties.config; | |||||
import lombok.Data; | import lombok.Data; | ||||
import org.springframework.beans.factory.annotation.Value; | import org.springframework.beans.factory.annotation.Value; | ||||
@@ -6,11 +6,11 @@ import org.springframework.stereotype.Component; | |||||
/** | /** | ||||
* <p> | * <p> | ||||
* 项目配置 | |||||
* 项目信息配置类 | |||||
* </p> | * </p> | ||||
* | * | ||||
* @author yangkai.shen | * @author yangkai.shen | ||||
* @date Created in 2018-09-29 10:50 | |||||
* @date Created in 2022-08-12 21:50 | |||||
*/ | */ | ||||
@Data | @Data | ||||
@Component | @Component |
@@ -1,4 +1,4 @@ | |||||
package com.xkcoding.properties.property; | |||||
package com.xkcoding.properties.config; | |||||
import lombok.Data; | import lombok.Data; | ||||
import org.springframework.boot.context.properties.ConfigurationProperties; | import org.springframework.boot.context.properties.ConfigurationProperties; | ||||
@@ -6,11 +6,11 @@ import org.springframework.stereotype.Component; | |||||
/** | /** | ||||
* <p> | * <p> | ||||
* 开发人员配置信息 | |||||
* 开发人员信息配置类 | |||||
* </p> | * </p> | ||||
* | * | ||||
* @author yangkai.shen | * @author yangkai.shen | ||||
* @date Created in 2018-09-29 10:51 | |||||
* @date Created in 2022-08-12 21:50 | |||||
*/ | */ | ||||
@Data | @Data | ||||
@ConfigurationProperties(prefix = "developer") | @ConfigurationProperties(prefix = "developer") | ||||
@@ -18,6 +18,5 @@ import org.springframework.stereotype.Component; | |||||
public class DeveloperProperty { | public class DeveloperProperty { | ||||
private String name; | private String name; | ||||
private String website; | private String website; | ||||
private String qq; | |||||
private String phoneNumber; | |||||
private String email; | |||||
} | } |
@@ -1,19 +1,19 @@ | |||||
package com.xkcoding.properties.controller; | package com.xkcoding.properties.controller; | ||||
import cn.hutool.core.lang.Dict; | import cn.hutool.core.lang.Dict; | ||||
import com.xkcoding.properties.property.ApplicationProperty; | |||||
import com.xkcoding.properties.property.DeveloperProperty; | |||||
import com.xkcoding.properties.config.ApplicationProperty; | |||||
import com.xkcoding.properties.config.DeveloperProperty; | |||||
import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
import org.springframework.web.bind.annotation.GetMapping; | import org.springframework.web.bind.annotation.GetMapping; | ||||
import org.springframework.web.bind.annotation.RestController; | import org.springframework.web.bind.annotation.RestController; | ||||
/** | /** | ||||
* <p> | * <p> | ||||
* 测试Controller | |||||
* 测试端点 | |||||
* </p> | * </p> | ||||
* | * | ||||
* @author yangkai.shen | * @author yangkai.shen | ||||
* @date Created in 2018-09-29 10:49 | |||||
* @date Created in 2022-08-12 21:12 | |||||
*/ | */ | ||||
@RestController | @RestController | ||||
public class PropertyController { | public class PropertyController { | ||||
@@ -1,34 +1,29 @@ | |||||
{ | { | ||||
"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" | |||||
} | |||||
] | |||||
} | |||||
"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.email", | |||||
"description": "The Developer QQ Number.", | |||||
"type": "java.lang.String" | |||||
} | |||||
] | |||||
} |
@@ -3,6 +3,5 @@ application: | |||||
version: dev环境 @version@ | version: dev环境 @version@ | ||||
developer: | developer: | ||||
name: dev环境 xkcoding | name: dev环境 xkcoding | ||||
website: dev环境 http://xkcoding.com | |||||
qq: dev环境 237497819 | |||||
phone-number: dev环境 17326075631 | |||||
website: dev环境 https://xkcoding.com | |||||
email: dev环境 237497819@qq.com |
@@ -3,6 +3,5 @@ application: | |||||
version: prod环境 @version@ | version: prod环境 @version@ | ||||
developer: | developer: | ||||
name: prod环境 xkcoding | name: prod环境 xkcoding | ||||
website: prod环境 http://xkcoding.com | |||||
qq: prod环境 237497819 | |||||
phone-number: prod环境 17326075631 | |||||
website: prod环境 https://xkcoding.com | |||||
email: prod环境 237497819@qq.com |
@@ -4,4 +4,4 @@ server: | |||||
context-path: /demo | context-path: /demo | ||||
spring: | spring: | ||||
profiles: | profiles: | ||||
active: prod | |||||
active: prod |
@@ -0,0 +1,13 @@ | |||||
package com.xkcoding.properties; | |||||
import org.junit.jupiter.api.Test; | |||||
import org.springframework.boot.test.context.SpringBootTest; | |||||
@SpringBootTest | |||||
class PropertiesApplicationTests { | |||||
@Test | |||||
void contextLoads() { | |||||
} | |||||
} |
@@ -1,16 +0,0 @@ | |||||
package com.xkcoding.properties; | |||||
import org.junit.Test; | |||||
import org.junit.runner.RunWith; | |||||
import org.springframework.boot.test.context.SpringBootTest; | |||||
import org.springframework.test.context.junit4.SpringRunner; | |||||
@RunWith(SpringRunner.class) | |||||
@SpringBootTest | |||||
public class SpringBootDemoPropertiesApplicationTests { | |||||
@Test | |||||
public void contextLoads() { | |||||
} | |||||
} |
@@ -10,7 +10,7 @@ | |||||
<modules> | <modules> | ||||
<module>common-tools</module> | <module>common-tools</module> | ||||
<module>demo-helloworld</module> | <module>demo-helloworld</module> | ||||
<!-- <module>demo-properties</module>--> | |||||
<module>demo-properties</module> | |||||
<!-- <module>demo-actuator</module>--> | <!-- <module>demo-actuator</module>--> | ||||
<!-- <module>demo-admin</module>--> | <!-- <module>demo-admin</module>--> | ||||
<!-- <module>demo-logback</module>--> | <!-- <module>demo-logback</module>--> | ||||