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.

Hopefully this article will help readers understand the ordered interface

Transferred from: 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.