[英]How to get configuration value for static value in Quarkus
我正在從舊系統重寫異常並且一切正常,但我需要使BAD_REQUEST
配置。
private static final String BAD_REQUEST = "BDRQ";
我試圖只放置 ConfigProperty,但它不起作用。
import javax.ws.rs.core.Response.Status;
import org.eclipse.microprofile.config.inject.ConfigProperty;
public class SXClientException extends RuntimeException {
@ConfigProperty(name = "greeting.error", defaultValue = "BDRQ")
public String BAD_REQUEST;
private final RuntimeException runtimeException;
public SXClientException(RuntimeException e) {
super(e);
this.runtimeException = e;
}
public Status getStatus() {
if (BAD_REQUEST.equals(runtimeException.getMessage())) {
return Status.BAD_REQUEST;
}
return Status.INTERNAL_SERVER_ERROR;
}
// ...
}
它可能不起作用,因為我在沒有任何 CDI 的情況下制作它們。
catch (LegacyOMException e) {
throw new SXClientException(e);
}
我寧願避免創建另一個 bean(並傳遞值)只是為了比較一個字符串。 知道如何讀取靜態值的配置值嗎?
您可以使用 org.eclipse.microprofile.config.ConfigProvider。 適用於靜態和非靜態成員。
public static final String BAD_REQUEST = ConfigProvider.getConfig().getValue("greeting.error",String.class);
public final String BAD_REQUEST = ConfigProvider.getConfig().getValue("greeting.error",String.class);
使用以下方法:
Properties properties = new Properties();
InputStream inputStream = this.getClass().getResourceAsStream("/menu.properties");
properties.load(inputStream );
System.out.println(properties.getProperty("a"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.