Исправить загрузку внешнего application.properties

This commit is contained in:
AidarKC 2026-06-25 18:04:04 +04:00
parent 84e0f039cb
commit f3e4233285
2 changed files with 22 additions and 7 deletions

View File

@ -3,6 +3,9 @@ package utils.config;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties; import java.util.Properties;
public final class AppConfig { public final class AppConfig {
@ -26,18 +29,30 @@ public final class AppConfig {
} }
private void load() { private void load() {
try (InputStream in = getClass().getClassLoader() try (InputStream in = getClass().getClassLoader().getResourceAsStream("application.properties")) {
.getResourceAsStream("application.properties")) {
if (in == null) { if (in == null) {
throw new RuntimeException("Config file application.properties not found"); throw new RuntimeException("Config file application.properties not found");
} }
properties.load(in); properties.load(in);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Failed to load application.properties", e); throw new RuntimeException("Failed to load application.properties", e);
} }
Path externalConfig = Paths.get("application.properties");
if (!Files.isRegularFile(externalConfig)) {
return;
}
Properties override = new Properties();
try (InputStream in = Files.newInputStream(externalConfig)) {
override.load(in);
} catch (IOException e) {
throw new RuntimeException("Failed to load external application.properties from " + externalConfig.toAbsolutePath(), e);
}
for (String name : override.stringPropertyNames()) {
properties.setProperty(name, override.getProperty(name));
}
} }
/** Вернёт значение строки или null, если параметр не найден */ /** Вернёт значение строки или null, если параметр не найден */

View File

@ -1,2 +1,2 @@
client.version=1.2.271 client.version=1.2.272
server.version=1.2.251 server.version=1.2.252