注释顺序重要吗? [英] Does annotations order matter?

查看:122
本文介绍了注释顺序重要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道,注释驱动的编程越来越多地并入我们当今使用的大多数框架(例如Spring,Lombok等)中.

As you know, the annotation driven programming is more and more incorporated in the majority of frameworks that we are using nowadays (i.e. Spring, Lombok etc).

此外,有时我们需要创建自定义注释.(例如,使用方面- @LogAroundMethods 记录给定类的所有公共方法的输入/退出跟踪)

Besides, we need sometimes to create our custom annotation. (i.g. Logging enter/exit traces for all public methods of a given class using an aspect - @LogAroundMethods)

因此,给定的类可以包含大量注释.

Hence, a given class can hold plenty of annotations.

@LogAroundMethod // My custom annotation
@Slf4j // Lombok annotation
@Component // Spring annotation
public class ClientNotificationProxy {
//Code
}

@LogAroundMethod // My custom annotation
@Configuration // Spring annotation
@ConditionalOnClass(NotificationSender.class) // Spring annotation
@EnableConfigurationProperties(MessagingProperties.class) // Spring annotation
@Import({ MongoConfiguration.class, SpringRetryConfiguration.class }) // Spring annotation
public class StarterClientAutoConfiguration {
// Code 
}

  • 推荐的注释顺序是什么?
  • 特定订单是否有影响或收益?
  • 推荐答案

    特定订单是否有影响或收益?

    Is there any impact or benefit from a specific order ?

    不是我所知道的.记住注释是如何工作的:一段代码看着"您的代码,并检查注释的存在.含义:它获取"注释数组,并检查它关心的注释是否在该数组中.因此,订单应该不相关.

    Not that I am aware of. Remember how annotations work: some piece of code "looks" at your code, and checks for the presence of an annotation. Meaning: it "gets" an array of annotations, and checks whether the one it cares about is in that array. So order should be irrelevant.

    当然,当我们谈论具有编译时效果的注释时,故事可能会有所不同.这样的注释会影响编译过程本身,因此在最坏的情况下,注释的顺序会更改编译过程.

    Of course, when we talk about annotations that have compile-time effects, the story might be different. Such annotations have effects on the compilation process itself, so worst case, order of annotations changes the compilation process.

    推荐的注释顺序是什么?

    What's the recommended order of annotations?

    最适合您的产品.含义:与您的团队坐下来,问问自己我们是否喜欢特定的订单".如果是这样,请将其写下来,并让人们遵守该规则.

    The one that works for you. Meaning: sit down with your team, and ask yourself "do we prefer a specific order". If so, write that down, and have people follow that rule.

    真实世界的示例:我们使用注释来描述"对象"的属性".在某些时候,我们看到添加新属性时经常忘记注释X.由于属性是按照随机顺序写下的,因此很难手动处理(而且我们有许多不同的注释,有时单个属性上会有5到10个注释).

    Real world example: we use annotations to "describe" the "properties" of our "objects". At some point, we saw that we often forgot annotation X when adding new properties. Because properties were written down in random order, thus hard to process manually (and we have plenty of different annotations, sometimes 5 to 10 on a single property).

    我们的解决方案":注释必须按字母顺序排序(用于此类属性"定义时).我们甚至有一个单元测试来检查该排序顺序.从那时起:所有属性"定义都遵循相同的规则.这对我们来说很好,因为每个人都有相同的期望和心态.

    Our "solution": annotations must be sorted alphabetically (when used for such "property" definitions). And we even have a unit test that checks for that sorting order. Since then: all "property" definitions follow the same rule. And that works nicely for us, because everybody comes with the same expectation and mindset.

    这篇关于注释顺序重要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆