Introduction to the ordered interface in spring

Source: Internet
Author: User
Tags sorts

Objective

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:

public interface Ordered {      int HIGHEST_PRECEDENCE = Integer.MIN_VALUE; int LOWEST_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 = (o2 instanceof PriorityOrdered); if (p1 && !p2) { return -1; } else if (p2 && !p1) {return 1; } int i1 = getOrder(o1); int i2 = 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: http://www.cnblogs.com/fangjian0423/p/springMVC-xml-json-convert.html#analysis

Since SPRINGMVC and the default are for us to inject the two classes of requestmappinghandleradapter and requestmappinghandlermapping, can we configure these two classes again?

The answer is of course possible.

Requestmappinghandlermapping:

<bean class=  "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" > <property name=" interceptors "> <bean class= "package.interceptor.XXInterceptor"/> </property> < Property name= "order" value=< Span class= "hljs-value" "-1"/></bean>    

Requestmappinghandleradapter:

<Beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" ><PropertyName="messageconverters" ><List><Beanclass="org.springframework.http.converter.ByteArrayHttpMessageConverter"/><Beanclass="org.springframework.http.converter.StringHttpMessageConverter" ><PropertyName="supportedmediatypes" ><List><Value>text/plain;charset=utf-8</value></list></property></bean><Beanclass="org.springframework.http.converter.ByteArrayHttpMessageConverter"/><Beanclass="org.springframework.http.converter.ResourceHttpMessageConverter"/><Beanclass="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/><Beanclass="org.springframework.http.converter.xml.MarshallingHttpMessageConverter" ><Constructor-argref="marshaller"/></bean></List></Property><PropertyName="customargumentresolvers" ><Beanclass= "org.format.demo.support.resolve.FormModelMethodArgumentResolver"/> </property> < property name= " Webbindinginitializer "> <bean class= " Org.format.demo.support.binder.MyWebBindingInitializer "/> </ property> <property name=" order "value= </bean>    

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.

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.

we see that when requestmappinghandlermapping and requestmappinghandleradapter do not have the Order property set, the default value of the Order property is Integer.max_ VALUE, which is the lowest Priority.

Ps:

If <mvc:annotation-driven/> is configured, a custom requestmappinghandleradapter is configured, and there is no setting The order value of the requestmappinghandleradapter, then the order value of the 2 Requestmappinghandleradapter is Integer.max_value. So who has a high priority? Answer: who first defined, who priority High. <mvc:annotation-driven/> Configuration before the custom requestmappinghandleradapter configuration, then <mvc:annotation-driven/ The Requestmappinghandleradapter priority of the > configuration is high, whereas the custom Requestmappinghandleradapter priority is High.

If <mvc:annotation-driven/&gt is configured, the customrequestmappinghandlermapping, and no settingsrequestmappinghandlermapping's order Value. Then the requestmappinghandlermapping priority of the <mvc:annotation-driven/> configuration is high because <mvc:annotation-driven/ > Internally sets the Requestmappinghandlermapping order, which is 0.

This reader is self-testing.

In multiple view interpreters, the ordered interface is also applied.

Summarize

The meaning of ordered interface in spring is understood, and the application of this interface is analyzed in Practice.

This ordered interface is also the landlord study Springmvc Configure multiple View parser when found, the previous time did not pay much attention, has always thought that the custom configuration Requestmappinghandleradapter priority will be higher, will overwrite <mvc: annotation-driven/> Configuration of Requestmappinghandleradapter. Now we understand the priority Issue.

Original: http://www.cnblogs.com/fangjian0423/p/spring-Ordered-interface.html

Introduction to the ordered interface in spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.