服务器之家

服务器之家 > 正文

SpringBoot之Order注解启动顺序说明

时间:2022-01-04 00:45     来源/作者:jiangxwa

Order注解启动顺序

order的规则

  • order的值越小,优先级越高
  • order如果不标注数字,默认最低优先级,因为其默认值是int最大值
  • 该注解等同于实现Ordered接口getOrder方法,并返回数字。
?
1
2
3
4
5
6
7
8
9
10
11
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Documented
public @interface Order {
 /**
  * The order value.
  * <p>Default is {@link Ordered#LOWEST_PRECEDENCE}.
  * @see Ordered#getOrder()
  */
 int value() default Ordered.LOWEST_PRECEDENCE;
}
?
1
int LOWEST_PRECEDENCE = Integer.MAX_VALUE;
?
1
2
3
4
5
6
7
8
@Aspect
@Component
public class DataSourceAspect implements Ordered {  
    @Override
    public int getOrder() {
        return 1;
    }
}

见下

OrderRunner1.java

?
1
2
3
4
5
6
7
8
@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

?
1
2
3
4
5
6
7
8
9
@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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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.");
 }
}

它们的启动日志

?
1
2
3
4
5
6
7
The service to start.
...
...
The OrderRunner1 start to initialize ...
The OrderRunner2 start to initialize ...
The Runner start to initialize ...
The service has started.

@Order注解提供消费顺序

Order注解可以做到集合bean依赖注入的时候,定义集合内部Bean的加载顺序,因此在需要有序消费bean的时候,不需要再次排序,直接定义好Order注解得value值就好。

但是这个order值并不影响bean本身实例化的顺序,因为实例化的顺序取决于依赖关系。

@org.springframework.core.annotation.Order

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@RestController
@RequestMapping(value = "/pc/api/v1/monitor")
@RequiredArgsConstructor
public class AfterRepairConsumer {
    private final List<RepairCreatePostConsumer> postConsumers;
    @GetMapping(value = "/create")
    public ResponseData create() {
        final String repairId = "1";
        
        if (CollectionUtils.isNotEmpty(postConsumers)) {
            postConsumers.forEach(e -> e.postHandler(repairId));
        }
        return new ResponseData<>("success");
    }
}
 
public interface RepairCreatePostConsumer {
    /**
     * 创建报修单后做什么
     *
     * @param repairId 报修单ID
     */
    void postHandler(String repairId);
 
import org.springframework.core.annotation.Order;
@Service
@RequiredArgsConstructor
@Order(value = 3)
public class SendEmail implements RepairCreatePostConsumer { 
    @Override
    public void postHandler(String repairId) {
        System.out.println("为报修单" + repairId + "发送邮件");
    }
}
 
import org.springframework.core.annotation.Order;
@Service
@RequiredArgsConstructor
@Order(value = 2)
public class SendInvoice implements RepairCreatePostConsumer {
    @Override
    public void postHandler(String repairId) {
        System.out.println("为报修单" + repairId + "发送发票");
    }
}
 
import org.springframework.core.annotation.Order;
@Service
@RequiredArgsConstructor
@Order(value = 1)
public class SendMessage implements RepairCreatePostConsumer {
    @Override
    public void postHandler(String repairId) {
        System.out.println("为报修单" + repairId + "发送消息");
    }

运行结果:

SpringBoot之Order注解启动顺序说明

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/jiangxwa/article/details/87892577

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部