關於Ordered接口,用過的人可能知道,這裏我談下自己的理解。也希望各位大神能給予指點。
源碼如下:
/**
* Interface that can be implemented by objects that should be
* orderable, for example in a Collection.
*
* <p>The actual order can be interpreted as prioritization, with
* the first object (with the lowest order value) having the highest
* priority.
*
* <p>Note that there is a 'priority' marker for this interface:
* {@link PriorityOrdered}. Order values expressed by PriorityOrdered
* objects always apply before order values of 'plain' Ordered values.
*
* @author Juergen Hoeller
* @since 07.04.2003
* @see OrderComparator
* @see org.springframework.core.annotation.Order
*/
public interface Ordered {
/**
* Useful constant for the highest precedence value.
* @see java.lang.Integer#MIN_VALUE
*/
int HIGHEST_PRECEDENCE = Integer.MIN_VALUE;
/**
* Useful constant for the lowest precedence value.
* @see java.lang.Integer#MAX_VALUE
*/
int LOWEST_PRECEDENCE = Integer.MAX_VALUE;
/**
* Return the order value of this object, with a
* higher value meaning greater in terms of sorting.
* <p>Normally starting with 0, with <code>Integer.MAX_VALUE</code>
* indicating the greatest value. Same order values will result
* in arbitrary positions for the affected objects.
* <p>Higher values can be interpreted as lower priority. As a
* consequence, the object with the lowest value has highest priority
* (somewhat analogous to Servlet "load-on-startup" values).
* @return the order value
*/
int getOrder();
}
當然這裏並不是無故提出這個接口來看的。事由是由PropertyPlaceholderConfigurer這個類的加載順序導致的。
這個類是實現了Ordered接口並且有這樣一句話
private int order = Ordered.LOWEST_PRECEDENCE; // default: same as non-Ordered
來說明PropertyPlaceholderConfigurer加載的順序,如果你細心的話,Ordered上面的一個註釋你會看到。
大概的翻譯就是值越小加載優先級越高,而普通的對象我們定義的是沒有實現Ordered接口的,即是他所說的
'plain' Ordered values.這樣的對象的加載優先級比實現Ordered接口優先級最低的還要低。就是說雖然
PropertyPlaceholderConfigurer類的優先級是實現Ordered接口中最低的,但也比plain Object的優先級高。所以我們在
定義PropertyPlaceholderConfigurer的時候,可以把位置放後面而先用其值。如圖: