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

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

问题描述

如您所知,注释驱动的编程越来越多地融入我们现在使用的大多数框架(即 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天全站免登陆