欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot之Order注解啟動順序說明

 更新時間:2021年09月15日 11:05:47   作者:jiangxwa  
這篇文章主要介紹了SpringBoot之Order注解啟動順序說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Order注解啟動順序

order的規(guī)則

  • order的值越小,優(yōu)先級越高
  • order如果不標注數(shù)字,默認最低優(yōu)先級,因為其默認值是int最大值
  • 該注解等同于實現(xiàn)Ordered接口getOrder方法,并返回數(shù)字。
@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; 
}
 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.");
 }
}

它們的啟動日志

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依賴注入的時候,定義集合內(nèi)部Bean的加載順序,因此在需要有序消費bean的時候,不需要再次排序,直接定義好Order注解得value值就好。

但是這個order值并不影響bean本身實例化的順序,因為實例化的順序取決于依賴關(guān)系。

@org.springframework.core.annotation.Order

@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 {
    /**
     * 創(chuàng)建報修單后做什么
     *
     * @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 + "發(fā)送郵件");
    }
}
 
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 + "發(fā)送發(fā)票");
    }
}
 
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 + "發(fā)送消息");
    }
}  
 

運行結(jié)果:

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論