티스토리 뷰

Programming/Spring

@Order 사용시 주의점

Albothyl 2021. 2. 8. 17:58

Spring에서 제공하는 @Order는 List<...> 형태로 DI 받을때 주입받는 Bean의 우선순위가 필요할때 사용한다.

@Order(value = 1), @Order(value = 2), @Order(value = 3)..

 

@Order의 value가 작을수록 우선순위가 높아 앞쪽으로 정렬되고, value가 클수록 우선순위가 낮아 뒷쪽으로 정렬된다.

 

이 때 주의할 점은 주입받는 N개의 Bean중 일부만 @Order를 사용하면 나머지Bean의 순서는 임의의 순서로 적용된다는 점이다.

 

List중 가장 마지막에 DI 되어야 하는 Bean이 있어서 이 Bean에만 @Order(Ordered.LOWEST_PRECEDENCE) 를 사용했다. Local에서는 이 Bean이 가장 나중에 나와 정상적으로 정렬이 되었는줄 알았지만, 운이 좋은거였다. 다른 사람의 Local이나 외부 개발서버에서는 결과가 달랐다. 그래서 Spring 문서를 찾아보니 아래와 같았다. OrderComparator 문서를 보면 이런 문구가 있다.

PriorityOrdered Objects
PriorityOrdered objects will be sorted with higher priority than plain Ordered objects.

Same Order Objects
Objects that have the same order value will be sorted with arbitrary ordering with respect to 
other objects with the same order value.

Non-ordered Objects
Any object that does not provide its own order value is implicitly assigned a value of 
Ordered.LOWEST_PRECEDENCE, thus ending up at the end of a sorted collection in 
arbitrary order with respect to other objects with the same order value.

 

* AnnotationAwareOrderComparator

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/annotation/Order.html

 

Order (Spring Framework 5.3.3 API)

@Order defines the sort order for an annotated component. The value() is optional and represents an order value as defined in the Ordered interface. Lower values have higher priority. The default value is Ordered.LOWEST_PRECEDENCE, indicating lowest priori

docs.spring.io

* OrderComparator

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/OrderComparator.html

 

OrderComparator (Spring Framework 5.3.3 API)

Determine a priority value for the given object, if any. The default implementation always returns null. Subclasses may override this to give specific kinds of values a 'priority' characteristic, in addition to their 'order' semantics. A priority indicates

docs.spring.io

 

'Programming > Spring' 카테고리의 다른 글

Spring Properties  (0) 2020.05.02
Spring SpEL (Expression Language)  (0) 2020.05.02
JDBC Template Query Logging  (0) 2019.08.02
RetryTemplate  (0) 2019.07.24
[주의] Controller Parameter Mapping Exception  (0) 2019.07.09
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함