Merge pull request #351 from ruansheng8/feat-aptConfig

feat: APT 支持从 Resources 目录进行配置
This commit is contained in:
Michael Yang 2024-06-12 17:05:13 +08:00 committed by GitHub
commit dfa3d708f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -50,6 +50,8 @@ public class MybatisFlexConfig {
protected final Properties properties = new Properties(); protected final Properties properties = new Properties();
public MybatisFlexConfig(Filer filer) { public MybatisFlexConfig(Filer filer) {
// 先从 resources 目录读取 mybatis-flex.config 配置文件
loadConfigFromResource();
try { try {
//target/classes/ //target/classes/
FileObject resource = filer.createResource(StandardLocation.CLASS_OUTPUT, "", "mybatis-flex"); FileObject resource = filer.createResource(StandardLocation.CLASS_OUTPUT, "", "mybatis-flex");
@ -97,6 +99,21 @@ public class MybatisFlexConfig {
} }
} }
private void loadConfigFromResource() {
Properties resourceProp = new Properties();
try (InputStream resourceStream = MybatisFlexConfig.class.getClassLoader().getResourceAsStream(APT_FILE_NAME)) {
if (resourceStream == null) {
return;
}
resourceProp.load(resourceStream);
if (resourceProp != null && !resourceProp.isEmpty()) {
properties.putAll(resourceProp);
}
} catch (Throwable e) {
e.printStackTrace();
}
}
public String get(ConfigurationKey key) { public String get(ConfigurationKey key) {
return properties.getProperty(key.getConfigKey(), key.getDefaultValue()); return properties.getProperty(key.getConfigKey(), key.getDefaultValue());
} }