简体   繁体   English

在春季重写父类受保护的成员变量注释

[英]Overriding parent class protected member variable annotations in spring

I came across the problem today using SpringBoot where I have a parent abstract class that defines a protected field like this with a spring annotation.. 今天,我使用SpringBoot遇到了问题,我有一个父抽象类,该抽象类定义了一个带有spring注释的受保护字段。

@Length(max=100)
protected String uuid;

In my subclass the max value for the @Length annotation needs to be set to a different value and I'm racking my brain on how to do this. 在我的子类中,需要将@Length批注的最大值设置为其他值,而我正在为如何做到这一点而绞尽脑汁。 After reading around I suspect there is a way to set annotations on class member variables in class constructors this way I could define the @Length annotation for the member variable uuid and then override the value in the child constructor but have not found any examples or documentation if this is even possible. 阅读后,我怀疑有一种方法可以在类构造函数中的类成员变量上设置注释,这样我可以为成员变量uuid定义@Length注释,然后覆盖子构造函数中的值,但未找到任何示例或文档如果可能的话。 Any ideas or examples on how to override parent protected variable annotations in a subclass using spring would be greatly appreciated. 任何有关如何使用spring覆盖子类中父级受保护变量注释的想法或示例,将不胜感激。

This is the full set of Annotations being used... 这是正在使用的全套注释...

@ApiModelProperty("Unique ID For My Object")
@Length(max=100)
@Pattern(regexp = "\\S*")
protected String uniqueId

In java there is no such thing as overriding of variables. 在Java中,没有诸如覆盖变量之类的东西。 Only methods can be overriden. 只能覆盖方法。 This last statement makes come an idea on how to solve the issue. 最后的声明提出了有关如何解决此问题的想法。 If you don't specify the annotation on the property directly but instead on the getter of the particular field like so for instance: 如果您不直接在属性上指定注释,而是在特定字段的getter上指定注释,例如:

class Parent{
   private String uniqueId;

   ...

   @ApiModelProperty("Unique ID For My Object")
   @Length(max=100)
   @Pattern(regexp = "\\S*")
   public String getUniqueId(){
      return uniqueId;
   }
}

Then you can override those properties in a child class by overriding the getter: 然后,您可以通过覆盖getter来覆盖子类中的那些属性:

class Child extends Parent{
   private String uniqueId;

   ...

   @ApiModelProperty("Unique ID For My Object")
   @Length(max=200)
   @Pattern(regexp = "\\S*")
   public String getUniqueId(){
      return uniqueId;
   }
}

I have not checked whether those annotation can be applied on getters also, but I would expect so. 我还没有检查这些注释是否也可以应用于吸气剂,但我希望如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM