Browse Source

property 完成

3.x
Yangkai.Shen 2 years ago
parent
commit
0f4576f5a9
13 changed files with 195 additions and 291 deletions
  1. +77
    -161
      demo-properties/README.md
  2. +57
    -58
      demo-properties/pom.xml
  3. +3
    -3
      demo-properties/src/main/java/com/xkcoding/properties/PropertiesApplication.java
  4. +3
    -3
      demo-properties/src/main/java/com/xkcoding/properties/config/ApplicationProperty.java
  5. +4
    -5
      demo-properties/src/main/java/com/xkcoding/properties/config/DeveloperProperty.java
  6. +4
    -4
      demo-properties/src/main/java/com/xkcoding/properties/controller/PropertyController.java
  7. +28
    -33
      demo-properties/src/main/resources/META-INF/additional-spring-configuration-metadata.json
  8. +2
    -3
      demo-properties/src/main/resources/application-dev.yml
  9. +2
    -3
      demo-properties/src/main/resources/application-prod.yml
  10. +1
    -1
      demo-properties/src/main/resources/application.yml
  11. +13
    -0
      demo-properties/src/test/java/com/xkcoding/properties/PropertiesApplicationTests.java
  12. +0
    -16
      demo-properties/src/test/java/com/xkcoding/properties/SpringBootDemoPropertiesApplicationTests.java
  13. +1
    -1
      pom.xml

+ 77
- 161
demo-properties/README.md View File

@@ -1,191 +1,107 @@
# spring-boot-demo-properties
## spring-boot-demo-properties

> 本 demo 演示如何获取配置文件的自定义配置,以及如何多环境下的配置文件信息的获取

## pom.xml
### 1.开发步骤
#### 1.1.添加依赖

```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
/**
* <p>
* 项目配置
* </p>
*
* @author yangkai.shen
* @date Created in 2018-09-29 10:50
*/
@Data
@Component
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
@ConfigurationProperties(prefix = "developer")
@Component
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
/**
* <p>
* 测试Controller
* </p>
*
* @author yangkai.shen
* @date Created in 2018-09-29 10:49
*/
@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("/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` ,重新启动,观察输出结果变化。

+ 57
- 58
demo-properties/pom.xml View File

@@ -1,75 +1,74 @@
<?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>
<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>
</resources>
</build>
</build>

</project>

demo-properties/src/main/java/com/xkcoding/properties/SpringBootDemoPropertiesApplication.java → demo-properties/src/main/java/com/xkcoding/properties/PropertiesApplication.java View File

@@ -9,12 +9,12 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* </p>
*
* @author yangkai.shen
* @date Created in 2018-09-29 10:48
* @date Created in 2022-08-12 21:12
*/
@SpringBootApplication
public class SpringBootDemoPropertiesApplication {
public class PropertiesApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootDemoPropertiesApplication.class, args);
SpringApplication.run(PropertiesApplication.class, args);
}
}

demo-properties/src/main/java/com/xkcoding/properties/property/ApplicationProperty.java → demo-properties/src/main/java/com/xkcoding/properties/config/ApplicationProperty.java View File

@@ -1,4 +1,4 @@
package com.xkcoding.properties.property;
package com.xkcoding.properties.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
@@ -6,11 +6,11 @@ import org.springframework.stereotype.Component;

/**
* <p>
* 项目配置
* 项目信息配置
* </p>
*
* @author yangkai.shen
* @date Created in 2018-09-29 10:50
* @date Created in 2022-08-12 21:50
*/
@Data
@Component

demo-properties/src/main/java/com/xkcoding/properties/property/DeveloperProperty.java → demo-properties/src/main/java/com/xkcoding/properties/config/DeveloperProperty.java View File

@@ -1,4 +1,4 @@
package com.xkcoding.properties.property;
package com.xkcoding.properties.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -6,11 +6,11 @@ import org.springframework.stereotype.Component;

/**
* <p>
* 开发人员配置信息
* 开发人员信息配置类
* </p>
*
* @author yangkai.shen
* @date Created in 2018-09-29 10:51
* @date Created in 2022-08-12 21:50
*/
@Data
@ConfigurationProperties(prefix = "developer")
@@ -18,6 +18,5 @@ import org.springframework.stereotype.Component;
public class DeveloperProperty {
private String name;
private String website;
private String qq;
private String phoneNumber;
private String email;
}

+ 4
- 4
demo-properties/src/main/java/com/xkcoding/properties/controller/PropertyController.java View File

@@ -1,19 +1,19 @@
package com.xkcoding.properties.controller;

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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* <p>
* 测试Controller
* 测试端点
* </p>
*
* @author yangkai.shen
* @date Created in 2018-09-29 10:49
* @date Created in 2022-08-12 21:12
*/
@RestController
public class PropertyController {


+ 28
- 33
demo-properties/src/main/resources/META-INF/additional-spring-configuration-metadata.json View File

@@ -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"
}
]
}

+ 2
- 3
demo-properties/src/main/resources/application-dev.yml View File

@@ -3,6 +3,5 @@ application:
version: dev环境 @version@
developer:
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

+ 2
- 3
demo-properties/src/main/resources/application-prod.yml View File

@@ -3,6 +3,5 @@ application:
version: prod环境 @version@
developer:
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

+ 1
- 1
demo-properties/src/main/resources/application.yml View File

@@ -4,4 +4,4 @@ server:
context-path: /demo
spring:
profiles:
active: prod
active: prod

+ 13
- 0
demo-properties/src/test/java/com/xkcoding/properties/PropertiesApplicationTests.java View File

@@ -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() {
}

}

+ 0
- 16
demo-properties/src/test/java/com/xkcoding/properties/SpringBootDemoPropertiesApplicationTests.java View File

@@ -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() {
}

}

+ 1
- 1
pom.xml View File

@@ -10,7 +10,7 @@
<modules>
<module>common-tools</module>
<module>demo-helloworld</module>
<!-- <module>demo-properties</module>-->
<module>demo-properties</module>
<!-- <module>demo-actuator</module>-->
<!-- <module>demo-admin</module>-->
<!-- <module>demo-logback</module>-->


Loading…
Cancel
Save