zoukankan      html  css  js  c++  java
  • @Component @PostConstruct @Order ApplicationRunner CommandLineRunner

    @Component中static优先于@PostConstruct优先于ApplicationRunner优先于CommandLineRunner
    1. @Component 优先执行
      • @Order可执行执行顺序:数字越小,优先级越高,也就是@Order(1)注解的类会在@Order(2)注解的类之前执行。
        @Order(value=1)
        @Order(value=2)
        @Order(value=3)
      • static 优先执行
      • @PostConstruct 在static完成后执行
    2. @Component implements ApplicationRunner 容器启动完成后 @Component完成后 执行
    3. @Component implements CommandLineRunner 容器启动完成 @Component完成后 ApplicationRunner 完成后 最后执行
    代码示例
    @Component @Order(value = 1) @PostConstruct
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    import javax.annotation.PostConstruct;
    
    @Component
    @Order(value = 1)
    public class Component1 {
    	
        static {
            System.out.println("
    
    Hello @Component Order1 static
    ");
        }
    	
        @PostConstruct
        public void start() {
            System.out.println("
    
    Hello @Component Order1 @PostConstruct
    ");
        }
    	
    }
    
    @Component @Order(value = 2) @PostConstruct
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    import javax.annotation.PostConstruct;
    
    @Component
    @Order(value = 2)
    public class Component2 {
    	
        static {
            System.out.println("
    
    Hello @Component Order2 static
    ");
        }
    	
        @PostConstruct
        public void start() {
            System.out.println("
    
    Hello @Component Order2 @PostConstruct
    ");
        }
    	
    }
    
    @Component implements ApplicationRunner
    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ComponentApplicationRunner implements ApplicationRunner{
    
        @Override
        public void run(ApplicationArguments args){
            System.out.println("
    
    Hello @Component implements ApplicationRunner
    ");
        }
    	
    }
    
    @Component implements CommandLineRunner
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ComponentCommandLineRunner implements CommandLineRunner{
    
        @Override
        public void run(String... strings){
            System.out.println("
    
    Hello @Component implements CommandLineRunner
    ");
        }
    	
    }
    
    I have a dream : Sandy beach B-J-N.
  • 相关阅读:
    【leetcode】496. Next Greater Element I
    工具网站
    [err]Traceback (most recent call last): File "/usr/local/bin/pip", line 7, in <module> from pip._internal import main ImportError: No module named 'pip._internal'
    []TLD code run
    【动手学深度学习】
    【论文阅读】Wing Loss for Robust Facial Landmark Localisation with Convolutional Neural Networks
    【linux基础】ubuntu系统NVIDIA驱动安装
    【linux基础】linux不能进入系统
    【leetcode】492. Construct the Rectangle
    【leetcode】485. Max Consecutive Ones
  • 原文地址:https://www.cnblogs.com/mjtabu/p/14949390.html
Copyright © 2011-2022 走看看