如何在方法签名中调用 application.yaml 参数?Java

huangapple go评论74阅读模式
英文:

How to call an application.yaml parameter in the method signature? Java

问题

我有一个方法,其签名是一个明确放置的字符串,但我想将这个字符串消息添加到 application.yaml 中并在方法中调用它,如下所示:

publishesPublic.replyRefusal(map, "500", "AC0001", "The request could not be processed at this time. Try again later.");

我如何将消息保留在 application.yaml 文件中并在方法中进行调用呢?

提前致谢!

英文:

I have a method whose signature is a String that is placed explicitly, but I would like to add this String message in the application.yaml and call it in the method, as shown below:

publishesPublic.replyRefusal(map, "500", "AC0001", "The request could not be processed at this time. Try again later.");

How do I leave the message in the application.yaml and call it in the method?

Thanks in advance!

答案1

得分: 0

应用的 YAML 文件将包含以下条目:

tryAgainMessage: 请稍后重试

在 Java 代码中进行注入如下:

@Value("${tryAgainMessage}")
private String tryAgainMessage;

使用它:

publishesPublic.replyRefusal(map, "500", "AC0001", tryAgainMessage);
英文:

Application yaml file will have the following entry.

tryAgainMessage: Try again later

Get it injected in java code as follows

@Value("${tryAgainMessage}")
private String tryAgainMessage;

Use it

publishesPublic.replyRefusal(map, "500", "AC0001", tryAgainMessage);

答案2

得分: 0

你可以将以下内容添加到 application.yml 文件中,使用一个键来定义:

error:
   message: 请求无法在此时处理。请稍后重试。

在你的 Bean 中,你可以像这样注入该值:

private String errorMessage;

public YourBean(@Value("${error.message}") String errorMessage) {
    this.errorMessage = errorMessage;
}

你也可以使用字段注入来进行注入,但是使用构造函数是更好的实践。

然后,你可以这样调用:

publishesPublic.replyRefusal(map, "500", "AC0001", errorMessage);
英文:

You can add it to application.yml with a key :

error:
   message: The request could not be processed at this time. Try again later.

And in you bean, you can inject the value like this :

private String errorMessage;

public YourBean(@Value("${error.message}") String errorMessage) {
    this.errorMessage = errorMessage;
}

You can inject it using field injection too, but using constructor is a better practice.

And then :

publishesPublic.replyRefusal(map, "500", "AC0001", errorMessage);

huangapple
  • 本文由 发表于 2020年9月14日 19:31:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63883424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定