Spring引导在Java注释上外部化配置属性/消息

编程入门 行业动态 更新时间:2024-10-15 08:24:10
本文介绍了Spring引导在Java注释上外部化配置属性/消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有办法在春天从外部属性文件中读取文本而不使用 @Value 注释。例如:application.properties

Is there a way to read text from external properties file in spring without using the @Value annotation. Ex: application.properties

var.foo="hello"

我可以使用

@Value("${var.foo}") String value;

作为类变量。有没有办法在不使用 @Value 注释的情况下包含此属性。类似于JSR bean验证的方式。

as a class variable. Is there a way to include this property without using the @Value annotation. Something like how JSR bean validation does.

@NotNull(message={custom.notnull})

并在 ValidationMessages.properties 文件中外部化此属性值。

and you externalize this property value in ValidationMessages.properties file.

例如,如果我有一个资源(Web组件)类,并且我必须使用Swagger注释来记录它们,

Example, if I have a resource(web component) class, and if I have to use Swagger annotations to document them,

@controller @path("/") @Api(description = "User Resource API") public class UserResource { @Path("/users") @GET @Produces(MediaType.APPLICATION_JSON) @ApiOperation(value = "returns user details", notes = "some notes") public User getUser() { return new User(123, "Joe", "Montana"); } }

和模型,

@ApiModel("user model") public class User { @ApiModelProperty(value = "This represents user id") private String id; private String firstName; private String lastName; ... }

如何外化此字符串/句子/消息到外部属性文件。我认为这适用于一般的Java注释和spring,而不是Swagger特有的。我指定Swagger的原因是,如果像hibernate验证一样,Java库可以选择在外部ValidationMessages.properties文件中指定这些消息,并且spring知道默认情况下可以选择它(或者可以配置)。

How do you externalize this string/sentences/messages to an external properties file. I think this applies to general Java annotations and spring and not specific to Swagger. The reason I specified Swagger is, if just like hibernate validation Java library has the option of specifying these messages in an external ValidationMessages.properties file and spring knows by default to pick it up (or can be configured).

Swagger是否提供这样的选择?如果没有,我该如何设置?

Does Swagger provide such an option? If it does not, how do I setup one?

基本问题是,我不想把我的代码/逻辑与文档相关的数据(元数据)混为一谈。

Underlying problem is, I don't want to clutter my code/logic with documentation related data(meta data).

推荐答案

我认为你不能实现这个目标,除非你使用的注释的底层实现是自己的i18n的标准(如 ValidationMessages.properties ),或支持Spring的 MessageSource 。

I don't think you can achieve this unless the underlying implementation of the annotations you're using either come with their own standard for i18n (like ValidationMessages.properties), or support Spring's MessageSource.

在后一种选择的情况下,您所要做的就是在 messages.properties 文件中添加您的键/值对,并让其余的框架:

In the case of the latter alternative, all you should have to do is to add your key/value pairs inside a messages.properties file and let the framework do the rest:

messages.properties

my.validation.error.message=Something went terribly wrong!

SomeClass.java

@MySpringCompatibleValidationAnnotation("my.validation.error.message") private String someVariable;

根据您尝试使用的框架,它可能支持也可能不支持这个开箱即用。

Bottom line is, depending on the framework you're trying to use, it may or may not support this out-of-the-box.

现在,就Swagger而言,i18n对文档的支持被​​提议作为一项新功能,但它被关闭或放入暂停时,他们会更多地考虑应该如何实施。请参阅 github/OAI/OpenAPI-Specification/issues/274了解更多详情。

Now, as far as Swagger goes, i18n support for documentation was proposed as a new feature, but it was shut down or put on hold while they put some more thought into how it should be implemented. See github/OAI/OpenAPI-Specification/issues/274 for more details.

更多推荐

Spring引导在Java注释上外部化配置属性/消息

本文发布于:2023-11-25 20:44:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631283.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   属性   消息   Spring   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!