[英]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还是直接读取字段?
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. 利益取决于注释所代表的内容。
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。
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.