6种方式
- main方法
- ApplicationListener<ApplicationStartedEvent>
- ApplicationListener<ApplicationReadyEvent>
- @PostConstruct
- ApplicationRunner
- CommandLineRunner
- InitializingBean
代码
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.wheelmouse</groupId>
<artifactId>studying</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>springboot-start-extension</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
启动类
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author wheelmouse
* @date 2024-03-10 5:07 PM
*/
@SpringBootApplication
public class SpringbootStartExtensionApplication implements ApplicationRunner, CommandLineRunner {
public static void main(String[] args) {
System.out.println("------main方法中run方法前------1");
SpringApplication.run(SpringbootStartExtensionApplication.class, args);
System.out.println("------main方法中run方法后------7");
}
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("------ApplicationRunner接口中run方法------4");
}
@Override
public void run(String... args) throws Exception {
System.out.println("======CommandLineRunner接口中run方法======5");
}
}
@PostConstruct
@Component
public class SpringBootStartPostConstruct {
@PostConstruct
public void init() {
System.out.println("======@PostConstruct注解中自定义的方法======2");
}
}
ApplicationListener<ApplicationStartedEvent>
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* @author wheelmouse
* @date 2024-03-10 6:37 PM
*/
@Component
public class SpringBootStartApplicationListener implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
System.out.println("======ApplicationListener<ApplicationStartedEvent>======3");
}
}
ApplicationListener<ApplicationReadyEvent>
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
/**
* @author wheelmouse
* @date 2024-03-10 6:40 PM
*/
@Component
public class SpringBootStartApplicationListener2 implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
System.out.println("======ApplicationListener<ApplicationReadyEvent>======6");
}
}
效果展示