SpringBoot the boot sequence annotation Order

order rules:

The smaller the value of the order, the higher the priority
order if the number is not marked, the lowest priority by default, because the default value is the maximum value of int
the annotation is equivalent to implement the method getOrder Ordered interface, and returns the number.

@Retention (RetentionPolicy.RUNTIME)
@Target ({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Documented
public @interface the Order {

/ **
* value of The Order.
* <P> {@link the Ordered the Default IS # . LOWEST_PRECEDENCE}
* @see the Ordered getOrder # ()
* /
int value () default Ordered.LOWEST_PRECEDENCE;

}
 

int LOWEST_PRECEDENCE = Integer.MAX_VALUE;
@Aspect
@Component
public class DataSourceAspect implements Ordered {

@Override
public int getOrder() {
return 1;
}

}
见下: 

OrderRunner1.java

@Component
@Order(1)
public class OrderRunner1 implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
System.out.println("The OrderRunner1 start to initialize ...");
}
}
 OrderRunner2.java

@Component
@Order(2)
public class OrderRunner2 implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
System.out.println("The OrderRunner2 start to initialize ...");
}
}
 Runner.java

@Component
public class Runner implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
System.out.println("The Runner start to initialize ...");
}
}
@SpringBootApplication
public class CommandLineRunnerApplication {

public static void main(String[] args) {
System.out.println("The service to start.");
SpringApplication.run(CommandLineRunnerApplication.class, args);
System.out.println("The service has started.");
}
}
 

They start the log:

Service to Start at The.
...
...
at The OrderRunner1 Start to the initialize ...
at The OrderRunner2 Start to the initialize ...
at The Runner Start to the initialize ...
at The Service has Started.
 
---------- -----------
author: jiangxwa
source: CSDN
original: https: //blog.csdn.net/jiangxwa/article/details/87892577
copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/shew/p/11258995.html