Introduction to the ordered interface in spring Catalogue Preface A ordered interface is provided in spring. Ordered interface, as the name implies, is used to sort. Spring is a framework that uses a lot of policy design patterns, which means that there are many implementations of the same interface, and there must be a priority problem. As a result, spring provides the ordered interface to handle the priority of the same interface implementation class. Ordered interface Introduction First, let's look at the definition of the ordered interface: {int highest_precedence = Integer.min_value; intlowest_precedence = integer.max_value; int GetOrder (); } There are only 1 methods: GetOrder (), 2 variables: superlative (minimum) and lowest (maximum value). Ordercomparator class: Implements a comparator for the comparator. Provides 3 static sorting methods: Sort (list<?> List), sort (object[] array), Sortifnecessary (Object value). Sorts the array and collection according to the Ordercomparator. The Sortifnecessary method internally determines whether the value parameter is object[] or the list type, and then sorts by using the sort method of the object[] parameter and the sort method of the list parameter. Let's look at the Compare method for this comparator: public int Compare (object O1, Object O2) {boolean p1 = (O1 instanceof priorityordered); Boolean P2 = (o2instanceof priorityordered); if (P1 &&!p2) {return-1;} else if (P2 &&!p1) {return 1;} int i1 = GetOrder (O1); Inti2 = GetOrder (O2); Return (I1 < I2)? -1: (I1 > I2)? 1:0; } Priorityordered is an interface that inherits from the ordered interface and does not define any methods. The logic of this piece of code: 1. If the object O1 is the ordered interface type, O2 is the Priorityordered interface type, then the O2 priority is higher than O1 2. If the object O1 is the Priorityordered interface type, O2 is the ordered interface type, then the O1 priority is higher than O2 3. In other cases, if both are ordered interface types or both are priorityordered interface types, call the GetOrder method of the ordered interface to get the order value, the higher the order value, the smaller the priority A simple summary is: When the Ordercomparator comparator is sorted, the object has a higher priority if there is an object in the 2 object that implements the Priorityordered interface. If 2 objects are implementation classes of the priorityordered or ordered interface, then the GetOrder method of comparing ordered interfaces gets the order value, the lower the value, the higher the priority. The use of the ordered interface in spring Taking Springmvc as an example, the application of ordered interface is illustrated. <mvc:annotation-driven/> If this configuration is defined in *-dispatcher.xml, then SPRINGMVC will inject the two classes of Requestmappinghandleradapter and requestmappinghandlermapping by default. For this part of the content, please refer to the landlord's other blog: Since SPRINGMVC and the default are for us to inject the two classes of Requestmappinghandleradapter and requestmappinghandlermapping, we can configure these two classes again. The answer is of course possible. Requestmappinghandlermapping: ===> Requestmappinghandleradapter: ====>text/plain;charset=utf-8> =====> =====> When we configured the Annotation-driven and the two beans. The spring container has 2 requestmappinghandleradapter and 2 requestmappinghandlermapping. Dispatcherservlet has handlermapping (requestmappinghandlermapping is its implementation class) set and Handleradapter ( Requestmappinghandleradapter is its implementation Class) collection. Private list Let's look at the initialization code for these two sets: It is obvious that we have used the Ordercomparator comparator we mentioned to sort. Let's look at the requestmappinghandlermapping and Requestmappinghandleradapter of the Annotation-driven code configuration. Requestmappinghandlermapping Default sets the Order property to 0,requestmappinghandleradapter without setting the Order property. Let's go inside the requestmappinghandlermapping and Requestmappinghandleradapter code to see how their order properties are defined. Abstracthandlermethodadapter is the parent class of Requestmappinghandleradapter. Abstracthandlermapping is the parent class of requestmappinghandlermapping. |