英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论