SpringBoot容器启动扩展点(开机启动)

6种方式

  1. main方法
  2. ApplicationListener<ApplicationStartedEvent>
  3. ApplicationListener<ApplicationReadyEvent>
  4. @PostConstruct
  5. ApplicationRunner
  6. CommandLineRunner
  7. InitializingBean

代码

  1. 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");
    }
}

效果展示
在这里插入图片描述

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值