Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does annotations order matter?

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).

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 
}
  • What's the recommended order of annotations ?
  • Is there any impact or benefit from a specific order ?
like image 642
M.Mas. Avatar asked Jan 03 '20 16:01

M.Mas.


People also ask

What is order annotation in spring?

The @Order annotation defines the sorting order of an annotated component or bean. It has an optional value argument which determines the order of the component; the default value is Ordered. LOWEST_PRECEDENCE. This marks that the component has the lowest priority among all other ordered components.

What is annotation and how it works?

An annotation is a construct associated with Java source code elements such as classes, methods, and variables. Annotations provide information to a program at compile time or at runtime based on which the program can take further action.

What is the order of bean creation in spring?

The order in which Spring container loads beans cannot be predicted. There's no specific ordering logic specification given by Spring framework. But Spring guarantees if a bean A has dependency of B (e.g. bean A has an instance variable @Autowired B b; ) then B will be initialized first.

Why should we use annotations?

Why Annotate? By annotating a text, you will ensure that you understand what is happening in a text after you've read it. As you annotate, you should note the author's main points, shifts in the message or perspective of the text, key areas of focus, and your own thoughts as you read.


2 Answers

In almost all cases the answer is No, the order has no effect.

But in fact it is a bit more complicated.

  1. Considering annotations that are processed by annotation processors, it was already stated in the other answers that it more depends on the order in which the processors run. However, the processors have access to the AST, which allows them to determine the order of annotations in the source code. So theoretically annotation processors can make the generated code dependent on the order, but I don't know any example for this and would call this bad practice.

  2. When getting the annotation of an element during runtime, you also have access to the order. The docs have more info on how the order is determined. So again, an implementation could make its behaviour dependent on the order. Again I would consider this bad practice. The only exception may be repeatable annotations, where I can think of use cases where this could be reasonable.

If there is any dependence on the order of annotations, which is very unlikely, this should be made very clear in the JavaDoc of the annotation.

So typically you are free to order them as you like. I don't know of any style guide on the order of annotations, so just make it reasonable for you.

like image 151
Jan Rieke Avatar answered Sep 23 '22 14:09

Jan Rieke


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.

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.

like image 30
GhostCat Avatar answered Sep 23 '22 14:09

GhostCat