Springboot @Order注解正确用法

结论

@Order注解不能指定 bean 的加载顺序,它适用于 AOP 的优先级,以及将多个 Bean 注入到集合时,这些 bean 在集合中的顺序。

Order注解控制Component加载顺序

注意:组件类必须实现接口

package com.wenxiaowu.springboot.order.component;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 测试组件order加载顺序(未实现接口,不能正确加载顺序)
 */
@SpringBootApplication(scanBasePackages = {"com.wenxiaowu.springboot.order.component"})
public class ComponentOrderTest {
    public static void main(String[] args) {
        System.out.println("ComponentOrderTest start.");
        SpringApplication.run(ComponentOrderTest.class, args);
        System.out.println("ComponentOrderTest end.");
    }
}

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

@Component
@Order(2)
class OrderRunner1 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("The OrderRunner1 start to initialize ...");
    }
}

@Component
@Order(1)
class OrderRunner2 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("The OrderRunner2 start to initialize ...");
    }
}

Order注解控制Service类加载顺序

package com.wenxiaowu.springboot.order.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 测试组件order加载顺序
 */
@SpringBootApplication(scanBasePackages = {"com.wenxiaowu.springboot.order.service"})
public class ServiceOrderTest {
    public static void main(String[] args) {
        System.out.println("ServiceOrderTest start.");
        SpringApplication.run(ServiceOrderTest.class, args);
        System.out.println("ServiceOrderTest end.");
    }
}

@Component
class AfterRepairConsumer {
    @Autowired
    private List<RepairCreatePostConsumer> postConsumers;

    @PostConstruct
    public void run() {
        final String repairId = "1";
        if (postConsumers != null && postConsumers.size() > 0) {
            postConsumers.forEach(e -> e.postHandler(repairId));
        }
    }
}

interface RepairCreatePostConsumer {
    /**
     * 创建报修单后做什么
     */
    void postHandler(String repairId);
}

@Service
@Order(value = 3)
class SendEmail implements RepairCreatePostConsumer {
    @Override
    public void postHandler(String repairId) {
        System.out.println("为报修单" + repairId + "发送邮件");
    }
}

@Service
@Order(value = 2)
class SendInvoice implements RepairCreatePostConsumer {
    @Override
    public void postHandler(String repairId) {
        System.out.println("为报修单" + repairId + "发送发票");
    }
}

@Service
@Order(value = 1)
class SendMessage implements RepairCreatePostConsumer {
    @Override
    public void postHandler(String repairId) {
        System.out.println("为报修单" + repairId + "发送消息");
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

文晓武

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值