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));
}
}
上一篇:转:Spring Boot启动过程


下一篇:Spring boot 启动过程解析 logback