简体   繁体   English

我应该在哪里放置字段/方法注释?

[英]Where should I place field/method annotations?

I appreciate that the answer to this could be 'just be consistent' but I'd like to understand whether there are advantages to adding annotations that apply to field/method to the field or the method. 我很欣赏这个问题的答案可能是“只是一致”,但我想了解在字段或方法中添加适用于字段/方法的注释是否有好处。 For example, are there any benefits of: 例如,有什么好处:

@XmlElement
private String str;

public String getString() {
    return str;
}

over: 过度:

private String str;

@XmlElement
public String getString() {
    return str;
}

Thanks. 谢谢。

It depends if there is logic in the get/set methods that you want to be executed during a marshal or unmarshal operation. 这取决于您是否希望在编组或解组操作期间执行get / set方法中的逻辑。

Example #1 - JPA Entities 示例#1 - JPA实体

Some JPA implementations use byte code manipulation to "weave" in code on the getter to perform lazy loading. 一些JPA实现使用字节码操作来“编织” getter上的代码以执行延迟加载。 If you are mapping this type of model to XML, then you will need to annotate the getter to ensure that the logic on the get methods is called. 如果要将此类型的模型映射到XML,则需要对getter进行注释以确保调用get方法上的逻辑。

Example #2 - Mismatched get/set Methods 示例#2 - 不匹配的get / set方法

In the answer below the user had non-compliant bean access methods. 在下面的答案中,用户具有不兼容的bean访问方法。 For that use case I recommended the use of field access. 对于该用例,我建议使用字段访问。

Example #3 - Read-Only Properties 示例#3 - 只读属性

If your object model has properties with a getter but no setter, then field access is useful. 如果您的对象模型具有带有getter但没有setter的属性,则字段访问非常有用。

For More Information 欲获得更多信息

That depends on what you're doing. 这取决于你在做什么。 For example: your xml serializer/converter uses the getter or read the field directly? 例如:您的xml序列化器/转换器使用getter还是直接读取字段?

  1. If it uses getter, mark the getter, since you can use any back logic for the getter, not only back field. 如果它使用getter,请标记getter,因为你可以使用任何后退逻辑来获取getter,而不仅仅是后场。
  2. If it read the field directly, mark the field, since the get is useless for the feature. 如果它直接读取该字段,请标记该字段,因为获取对该功能没有用。 The getter may change in the future but the field won't and cannot change. 吸气剂可能在未来发生变化,但该领域不会也不会改变。

In the first case, access to the annotated element will be performed using reflexion as the field is private. 在第一种情况下,由于字段是私有的,因此将使用反射执行对带注释元素的访问。 This may lead to less optimal performance (to be checked on a real live case). 这可能会导致最佳性能降低(需要在实际情况下进行检查)。

In the second case, the access will be performed using the getter/setter. 在第二种情况下,将使用getter / setter执行访问。

In Hibernate, for instance, annotating a private attribute can create true read-only attribute: 例如,在Hibernate中,注释私有属性可以创建真正的只读属性:

@Id
private int id;

// No setter so it cannot be changed without reflexion
public int getId() {
    return id;
}

On the other hand, annotating the accessors will give you the opportunity to do more than merely setting/getting an attribute. 另一方面,注释访问器将使您有机会做更多的事情,而不仅仅是设置/获取属性。

The benefit dependends on what the annotation represents. 利益取决于注释所代表的内容。

  1. Many frameworks allow field/method annotations to complement each other, such as setter injection in the Spring framework. 许多框架允许字段/方法注释相互补充,例如Spring框架中的setter注入。 There field annotations lets you decide if a setter should be present or not. 使用字段注释可以决定是否应该存在setter。

  2. Other annotation processors might trigger different behaviour based on if it's a method or field which is annotated. 其他注释处理器可能会触发不同的行为,具体取决于它是注释的方法还是字段。 For instance, a security annotation might be put on method control authorization to that method, while a field annotation might control authorization to all routines referencing the variable. 例如,安全注释可以放在对该方法的方法控制授权上,而字段注释可以控制对所有引用该变量的例程的授权。

But generally the option in my opinion is most likely that of preference , or style if you wish. 但总的来说,我认为该选项很可能是偏爱的选项 ,或者您愿意的话,可以选择样式

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

相关问题 方法评论和注释......每个应该去哪里? - Method comments and annotations… where should each go? 我应该把包裹放在哪里? - Where should i place the packages? 当路径为“./”时,我应该在哪里放置文件? - Where should I place a file when the path is "./"? 我应该在哪里放置count变量? - where should I place the count variable? 我应该在哪里放置ACRA.init(this);? - Where should I place ACRA.init(this);? 当单击鼠标时,应在main方法的哪里放置新的mouseListener来向CarShape左右动画? - Where should I place a new mouseListener in the main method to animate CarShape left or right when the mouse is clicked? 春季事务管理-注释在哪里放置? - Spring Transactional Management - Where to Place the Annotations? 我应该传递参数还是将它们注入需要它们的地方? - Should I pass parameters or inject them to the place where they are needed? 我应该将pdf放在Java Web应用程序项目中的哪里? - Where should I place a pdf to be download in a Java web app project? 我应该在哪里放置需要访问数据库的验证码? - Where should I place validation code that requires access to database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM