有沒有方法可以在不使用@Value
註釋的情況下從外部屬性文件中讀取文本。例如:爲application.propertiesSpring引導將Java註釋中的配置屬性/消息外部化
var.foo="hello"
我可以在一個Spring bean使用
@Value("${var.foo}") String value;
爲類變量注入它。有沒有辦法在不使用@Value
註釋的情況下包含此屬性。就像JSR bean驗證一樣。
@NotNull(message={custom.notnull})
,你在外部化ValidationMessages.properties文件該屬性值。
例如,如果我有一個資源(網絡組件)類,如果我不得不使用揚鞭註釋記錄他們,
@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默認會選擇它(或可以配置)。
Swagger是否提供這樣的選項?如果沒有,我如何設置一個?
潛在的問題是,我不想混淆我的代碼/邏輯與文檔相關的數據(元數據)。
您需要提供關於您的用例的更多細節,以瞭解爲什麼您不能或不想使用'@ Value'註釋。 Spring的其他領域圍繞i18n支持,它使用messages.properties來允許某些區域利用語言特定的文本來替換令牌。 –
嗨@shawn ..感謝您的回覆。讓我編輯這個問題 – Bharath