forked from halo-dev/halo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support configuration properties mechanism for plugin in Halo core (h…
…alo-dev#4043) #### What type of PR is this? /kind feature /area core /area plugin #### What this PR does / why we need it: This PR adds property sources into PluginApplicationContext environment to support configuration properties mechanism. See halo-dev#4015 for more. #### Which issue(s) this PR fixes: Fixes halo-dev#4015 #### Special notes for your reviewer: You can verify the mechanism in [plugin-starter](https://github.com/halo-dev/plugin-starter) according to documentation `docs/developer-guide/plugin-configuration-properties.md`. I've only tested it on macOS, looking forward to feedback on Windows. #### Does this PR introduce a user-facing change? ```release-note 支持在插件中定义 @ConfigurationProperties 注解 ```
- Loading branch information
Showing
2 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# 插件外部配置 | ||
|
||
插件外部配置功能允许用户在特定目录添加插件相关的配置,插件启动的时候能够自动读取到该配置。 | ||
|
||
## 配置优先级 | ||
|
||
> 优先级从上到下由高到低。 | ||
1. `${halo.work-dir}/plugins/configs/${plugin-id}.{yaml|yml}` | ||
2. `classpath:/config.{yaml|yml}` | ||
|
||
插件开发者可在 `Class Path` 下 添加 `config.{yaml|yml}` 作为默认配置。当 `.yaml` 和 `.yml` 同时出现时,以 `.yml` 的配置将会被忽略。 | ||
|
||
## 插件中定义配置并使用 | ||
|
||
- `src/main/java/my/plugin/MyPluginProperties.java` | ||
|
||
```java | ||
@Data | ||
@ConfigurationProperties | ||
public class MyPluginProperties { | ||
|
||
private String encryptKey; | ||
|
||
private String certPath; | ||
} | ||
``` | ||
|
||
- `src/main/java/my/plugin/MyPluginConfiguration.java` | ||
|
||
```java | ||
@EnableConfigurationProperties(MyPluginProperties.class) | ||
@Configuration | ||
public class MyPluginConfiguration { | ||
|
||
} | ||
``` | ||
|
||
- `src/main/java/my/plugin/MyPlugin.java` | ||
|
||
```java | ||
@Component | ||
@Slf4j | ||
public class MyPlugin extends BasePlugin { | ||
|
||
private final MyPluginProperties storeProperties; | ||
|
||
public MyPlugin(PluginWrapper wrapper, MyPluginProperties storeProperties) { | ||
super(wrapper); | ||
this.storeProperties = storeProperties; | ||
} | ||
|
||
@Override | ||
public void start() { | ||
log.info("My plugin properties: {}", storeProperties); | ||
} | ||
} | ||
``` | ||
|
||
- `src/main/resources/config.yaml` | ||
|
||
```yaml | ||
encryptKey: encrytkey== | ||
certPath: /path/to/cert | ||
``` | ||
|
||
## 插件使用者配置 | ||
|
||
- `${halo.work-dir}/plugins/configs/${plugin-id}.{yaml|yml}` | ||
|
||
```yaml | ||
encryptKey: override encrytkey== | ||
certPath: /another/path/to/cert | ||
``` | ||
|
||
## 可能存在的问题 | ||
|
||
- 增加未来实现"集群"架构的难度。 |