This is the seventh day of my participation in the August More Text Challenge. For details, see: August More Text Challenge
The default value is INT_MAX. The lower the value is, the higher the priority is. It does not affect the start Order. Ordered interface getOrder: Ordered interface getOrder: Ordered interface getOrder: Ordered interface getOrder
public interface Ordered {
int HIGHEST_PRECEDENCE = Integer.MIN_VALUE;
int LOWEST_PRECEDENCE = Integer.MAX_VALUE;
// This is similar to the @order annotation, except that one is an annotation and the other is based on the interface
int getOrder(a);
Copy the code
Then use the OrderUtils abstract tools classes encapsulate processing, considering the performance problems, here the spring with a subclass of Map ConcurrentReferenceHashMap local cache. It is described in the source code for this subclass as follows: If not explicitly specified, this implementation defaults to using soft references (Java reference types). The purpose of this reference type is to tell the GC what is not important when memory is running out. Soft reference types are one such class. If there is still not enough memory after the collection, then the OutOfMemory error will be thrown. Soft reference types are very suitable for caching scenarios, so you can try them out if you are interested. Ok, we continue to don’t pull too far, it is ultimately through AnnotationAwareOrderComparator. Sort () implementation, and AnnotationAwareOrderComparator inherited OrderComparator class, OrderComparator, in turn, implements the java.util.Comparator interface for sorting, which is how it works. Ok, let’s verify that
Create a new project and import only the following Maven packages
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
Copy the code
/ * * *@author dongqin
* @descriptionThe order annotation comparator tests *@date2021/08/06 * /
public class AnnotationAwareOrderComparatorTest {
private final static Log LOG = LogFactory.getLog(AnnotationAwareOrderComparatorTest.class);
private static List<Object> listObject = new ArrayList<>(2);
public static void main(String[] args) {
listObject.add(new Role());
listObject.add(new User());
// Log input the result before sorting
doPrintLogByIndex(0);
doPrintLogByIndex(1);
if (LOG.isInfoEnabled()) {
LOG.info("Start [AnnotationAwareOrderComparator# sort the List (List)]");
}
// Sort
AnnotationAwareOrderComparator.sort(listObject);
// Log input the sorted result
doPrintLogByIndex(0);
doPrintLogByIndex(1);
}
/** * Print logs by index **@paramThe index index * /
public static void doPrintLogByIndex(int index) {
Object o = listObject.get(index);
if (o instanceof User) {
if (LOG.isInfoEnabled()) {
LOG.info(String.format([%d] executes class: [%s]", ++ index , User.class.getName()));
}
return;
}
if (LOG.isInfoEnabled()) {
StringBuffer buffer = new StringBuffer();
buffer.append("The first [");
buffer.append(++ index);
buffer.append("] is a class that executes: [");
buffer.append(Role.class.getName());
buffer.append("]"); LOG.info(buffer.toString()); }}}@Order(-200)
class User {
@Override
public String toString(a) {
return "User{}"; }}@Order(2)
class Role {
@Override
public String toString(a) {
return "Role{}"; }}Copy the code
Run the main method, and the result is as follows
Connected to the target VM, address: '127.0.0.1:20869', transport: 'socket'August07.2021 12:37:06Morning com. Dqcer. Dxptools. Core. Order. AnnotationAwareOrderComparatorTest doPrintLogByIndex information: the first [1] is a class of execution: [. Com. Dqcer dxptools. Core. Order. Role] in August07.2021 12:37:06Morning com. Dqcer. Dxptools. Core. Order. AnnotationAwareOrderComparatorTest doPrintLogByIndex information: the first [2] is a class of execution: [. Com. Dqcer dxptools. Core. The order. The User] in August07.2021 12:37:06Morning com. Dqcer. Dxptools. Core. Order. AnnotationAwareOrderComparatorTest main information: Begin to execute [AnnotationAwareOrderComparator# sort the List (List)] in August07.2021 12:37:06Morning com. Dqcer. Dxptools. Core. Order. AnnotationAwareOrderComparatorTest doPrintLogByIndex information: the first [1] is a class of execution: [. Com. Dqcer dxptools. Core. The order. The User] in August07.2021 12:37:06Morning com. Dqcer. Dxptools. Core. Order. AnnotationAwareOrderComparatorTest doPrintLogByIndex information: the first [2] is a class of execution: [. Com. Dqcer dxptools. Core. Order. Role] Disconnected from the target VM, address:'127.0.0.1:20869', transport: 'socket'
Process finished with exit code 0
Copy the code
Through the practice proves that through AnnotationAwareOrderComparator. Sort () does sort function.