Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suggested Java annotation order?

For projects heavily used of Java annotations, is there any suggestion on the annotation order? So as to uniform the code style in the team.

like image 839
Xiè Jìléi Avatar asked Apr 12 '11 16:04

Xiè Jìléi


People also ask

Does order of annotations matter in Java?

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

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 the order of bean creation in spring?

The order of bean initialization is same as it's defined in the spring bean configuration file. The context is returned only when all the spring beans are initialized properly with post-init method executions. Employee name is printed as “Pankaj” because it was initialized in the post-init method.

What is @primary annotation in spring boot?

@Primary indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. There should be only one @Primary bean among same type of beans.


2 Answers

I'm going to say there's not. I could be proven wrong on this.

My reasoning is there are hundreds of projects that have annotations, in addition to the ones baked into Java, and the ones baked into our IDEs (specifically Eclipse but I'm sure others have them too.)

So, with all these libraries potentially competitng for "whose on top" I doubt they would agree on a standard.

I would encourage your team to sit down and make decisions about what's best for you guys.

Factors I would consider:

  1. Which annotatiosn are most important? They should be near the top.
  2. Which annotations are most likely to be indicitive of potential bugs? They should be near the top.
  3. Which annotations are likely to be on every method in the class? They should be near the bottom.
like image 61
corsiKa Avatar answered Oct 11 '22 06:10

corsiKa


If you must impose an order, alphabetical?

Without some sort of knowledge about the annotations you are using, it's hard to suggest a rational ordering. Perhaps a better ordering would suit you and your team better; and if so, if you document what's the agreed order, the only thing left is to verify it follows the team agreed ordering.

After talking to your whole team, you might find you don't need to order them after all.

like image 23
Edwin Buck Avatar answered Oct 11 '22 06:10

Edwin Buck