zoukankan      html  css  js  c++  java
  • Spring Boot 启动过程及 自定义 Listener等组件

    一、启动过程


    二、自定义组件


    package com.example.jdbc.listener;
    import org.springframework.context.ApplicationContextInitializer;
    import org.springframework.context.ConfigurableApplicationContext;
    
    public class HelloApplicationContextInitiallizer implements ApplicationContextInitializer {
        @Override
        public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
            System.out.println("HelloApplicationContextInitiallizer.initialize");
        }
    }
    package com.example.jdbc.listener;
    
    import org.springframework.boot.SpringApplicationRunListener;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.core.env.ConfigurableEnvironment;
    
    public class HelloSpringApplicationRunListener implements SpringApplicationRunListener {
    
        @Override
        public void environmentPrepared(ConfigurableEnvironment environment) {
            System.out.println("HelloSpringApplicationRunListener...environmentPrepared...");
        }
    
        @Override
        public void contextPrepared(ConfigurableApplicationContext context) {
            System.out.println("HelloSpringApplicationRunListener.contextPrepared");
        }
    
        @Override
        public void contextLoaded(ConfigurableApplicationContext context) {
            System.out.println("HelloSpringApplicationRunListener.contextLoaded");
        }
    
        @Override
        public void started(ConfigurableApplicationContext context) {
            System.out.println("HelloSpringApplicationRunListener.started");
        }
    
        @Override
        public void running(ConfigurableApplicationContext context) {
            System.out.println("HelloSpringApplicationRunListener.running");
        }
    
        @Override
        public void failed(ConfigurableApplicationContext context, Throwable exception) {
            System.out.println("HelloSpringApplicationRunListener.failed");
        }
    
        @Override
        public void starting() {
            System.out.println("HelloSpringApplicationRunListener.starting");
        }
    }

    /src/main/resources/META-INF/spring.factories

    org.springframework.context.ApplicationContextInitializer=
    com.example.jdbc.listener.HelloApplicationContextInitiallizer
    
    org.springframework.context.ApplicationListener=
    com.example.jdbc.listener.HelloSpringApplicationRunListener

    以上两个组件在项目启动过程中未能够成功执行,待解

    以下两个组件成功加载并在项目启东时执行


    package com.example.jdbc.listener;
    
    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.stereotype.Component;
    
    @Component
    public class HelloApplicationRunner implements ApplicationRunner {
    
        @Override
        public void run(ApplicationArguments args) throws Exception {
            System.out.println("HelloApplicationRunner.run");
        }
    }
    package com.example.jdbc.listener;
    
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.stereotype.Component;
    import java.util.Arrays;
    
    @Component
    public class HelloCommandLineRunner implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            System.out.println("HelloCommandLineRunner.run" + Arrays.asList(args));
        }
    }
  • 相关阅读:
    怎样才是全能的程序员?
    [HDU 2553]N皇后问题
    [HDU 1870]愚人节的礼物
    [HDU 1016]Prime Ring Problem
    [HDU 1241]Oil Deposits
    [POJ 3250]Bad Hair Day
    [HDU 1276]士兵队列训练问题
    [POJ 2796]Feel Good
    [HDU 1237] 简单计算器
    总算开通博客园啦~
  • 原文地址:https://www.cnblogs.com/guchunchao/p/10062597.html
Copyright © 2011-2022 走看看