• org.springframework.core.Ordered接口


    关于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的时候,可以把位置放后面而先用其值。如图:




  • 相关阅读:
    【ASP.NET Web API教程】5.5 ASP.NET Web API中的HTTP Cookie
    【ASP.NET Web API教程】3.3 通过WPF应用程序调用Web API(C#)
    【ASP.NET Web API教程】5.4 ASP.NET Web API批处理器
    【翻译】ASP.NET Web API是什么?
    【ASP.NET Web API教程】2.3.6 创建产品和订单控制器
    《精通ASP.NET MVC 3框架》译者序
    【ASP.NET Web API教程】3 Web API客户端
    委托、事件与匿名方法 — 学习委托最好的资料
    【ASP.NET Web API教程】2.3.3 创建Admin控制器
    【ASP.NET Web API教程】2.3.5 用Knockout.js创建动态UI
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3313324.html
Copyright © 2020-2023  润新知