【企业项目实战】Spring Boot 启动时加载指定方法

?博主介绍: 博主从事应用安全和大数据领域,有8年研发经验,5年面试官经验,Java技术专家,WEB架构师,阿里云专家博主,华为云云享专家,51CTO TOP红人

Java知识图谱点击链接:体系化学习Java(Java面试专题)

?? 感兴趣的同学可以收藏关注下不然下次找不到哟??

✊✊ 感觉对你有帮助的朋友,可以给博主一个三连,非常感谢 ???

在这里插入图片描述

@[TOC]

写在前面

首先先介绍下项目启动时就加载指定方法的这个需求对应的场景。场景的场景如下:
? 1、初始化数据:项目表结构升级了,增加了字段,存量的客户那边还是老版本,升级后,必定这些字段都是空数据,那么就需要启动时通过应用程序给这些字段填充数据(当然有些项目是通过调度器的)。

? 2、缓存预热:项目启动时,我们需要初始化某些数据到缓存中。

? 3、启动时加载配置:项目启动时,需要从配置中心加载配置,或者将配置信息加载到远端。

? 4、任务调度:有些自己写的调度器,是项目启动时需要加载的。

? 5、启动时校验:有些项目启动时需要做很多校验,可能是程序上的(如:sql校验),也可能是业务上的。

1、Spring Boot 启动时加载指定方法都有哪些方式的?

常用的方式有以下几种:

? 1. 实现 CommandLineRunner 接口或 ApplicationRunner 接口:这两个接口提供了一个 run 方法,在应用程序启动后立即执行。您可以在这个方法中编写您希望在应用程序启动时执行的代码。

? 2. 使用 @PostConstruct 注解:您可以将 @PostConstruct 注解添加到自定义类的方法上,该方法将在该类的实例被创建后立即执行。这样,您可以在该方法中编写您希望在应用程序启动时执行的代码。

? 3. 使用 ApplicationListener 接口:您可以实现 ApplicationListener 接口,并监听 ApplicationStartedEvent 事件。当应用程序启动时,该事件将被触发,您可以在监听器中编写您的自定义逻辑。

? 4. 使用 @EventListener 注解:您可以将 @EventListener 注解添加到自定义类的方法上,并指定要监听的事件类型。当指定的事件发生时,该方法将被调用,您可以在其中编写您的自定义逻辑。例如,您可以监听 ApplicationStartedEvent 事件。

2、CommandLineRunner 方式

CommandLineRunner 接口是Spring Boot提供的一个回调接口,可以在应用程序启动后执行特定的方法。您可以创建一个实现 CommandLineRunner 接口的类,并重写 run 方法,在其中编写需要在启动时执行的逻辑。

使用代码如下:

package com.pany.camp.load;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
*
* @description: CommandLineRunner
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: pany
* @version: 1.0.0
* @createTime: 2023-07-06 21:54
*/
@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// 在启动时执行的逻辑
System.out.println("应用程序启动时执行的方法");
}
}
package com.pany.camp.load;









import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;


/**

 *

 * @description:  CommandLineRunner
 * @copyright: @Copyright (c) 2022 
 * @company: Aiocloud

 * @author: pany

 * @version: 1.0.0 
 * @createTime: 2023-07-06 21:54
 */

@Component

public class MyCommandLineRunner implements CommandLineRunner {


    @Override
    public void run(String... args) throws Exception {
        // 在启动时执行的逻辑

        System.out.println("应用程序启动时执行的方法");

    }

}
package com.pany.camp.load; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; /** * * @description: CommandLineRunner * @copyright: @Copyright (c) 2022 * @company: Aiocloud * @author: pany * @version: 1.0.0 * @createTime: 2023-07-06 21:54 */ @Component public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { // 在启动时执行的逻辑 System.out.println("应用程序启动时执行的方法"); } }

3、ApplicationRunner 方式

ApplicationRunner 接口与 CommandLineRunner 接口类似,也是Spring Boot提供的一个回调接口,用于在应用程序启动后执行特定的方法。与 CommandLineRunner 不同的是, ApplicationRunner 的 run 方法接收一个 ApplicationArguments 对象,可以用于获取应用程序启动参数。

使用代码如下:

package com.pany.camp.load;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
/**
*
* @description: ApplicationRunner
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: pany
* @version: 1.0.0
* @createTime: 2023-07-06 22:04
*/
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
// 在启动时执行的逻辑
System.out.println("应用程序启动时执行的方法");
}
}
package com.pany.camp.load;









import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;





/**


 *


 * @description:  ApplicationRunner
 * @copyright: @Copyright (c) 2022
 * @company: Aiocloud


 * @author: pany


 * @version: 1.0.0
 * @createTime: 2023-07-06 22:04
 */


@Component


public class MyApplicationRunner implements ApplicationRunner {
    
    @Override

    public void run(ApplicationArguments args) throws Exception {
        // 在启动时执行的逻辑
        System.out.println("应用程序启动时执行的方法");
    }


}
package com.pany.camp.load; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; /** * * @description: ApplicationRunner * @copyright: @Copyright (c) 2022 * @company: Aiocloud * @author: pany * @version: 1.0.0 * @createTime: 2023-07-06 22:04 */ @Component public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { // 在启动时执行的逻辑 System.out.println("应用程序启动时执行的方法"); } }

4、@PostConstruct 方式

@PostConstruct 注解是Java EE提供的一个标准注解,用于指定在构造函数执行完毕后立即执行的方法。在Spring Boot中,您可以在任何一个Bean中使用 @PostConstruct 注解来标记需要在启动时执行的方法。

使用代码如下:

package com.pany.camp.load;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
*
* @description: PostConstruct
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: pany
* @version: 1.0.0
* @createTime: 2023-07-06 22:07
*/
@Component
public class MyPostConstruct {
@PostConstruct
public void init() {
// 在启动时执行的逻辑
System.out.println("应用程序启动时执行的方法");
}
}
package com.pany.camp.load;









import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;


/**

 *

 * @description: PostConstruct
 * @copyright: @Copyright (c) 2022
 * @company: Aiocloud

 * @author: pany

 * @version: 1.0.0
 * @createTime: 2023-07-06 22:07
 */

@Component

public class MyPostConstruct {


    @PostConstruct
    public void init() {
        // 在启动时执行的逻辑

        System.out.println("应用程序启动时执行的方法");

    }

}
package com.pany.camp.load; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; /** * * @description: PostConstruct * @copyright: @Copyright (c) 2022 * @company: Aiocloud * @author: pany * @version: 1.0.0 * @createTime: 2023-07-06 22:07 */ @Component public class MyPostConstruct { @PostConstruct public void init() { // 在启动时执行的逻辑 System.out.println("应用程序启动时执行的方法"); } }

5、ApplicationListener 方式

ApplicationListener 是 Spring Framework 提供的一个接口,用于监听应用程序中的事件并执行相应的逻辑。您可以实现 ApplicationListener 接口,并重写 onApplicationEvent 方法来处理特定的事件。

使用代码如下:

package com.pany.camp.load;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
*
* @description: ApplicationListener
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: pany
* @version: 1.0.0
* @createTime: 2023-07-06 22:09
*/
@Component
public class MyEventListener implements ApplicationListener<ApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent event) {
// 在这里处理特定的事件逻辑
System.out.println("收到应用程序事件:" + event.toString());
}
}
package com.pany.camp.load;









import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;





/**


 *


 * @description: ApplicationListener
 * @copyright: @Copyright (c) 2022 

 * @company: Aiocloud


 * @author: pany


 * @version: 1.0.0 

 * @createTime: 2023-07-06 22:09
 */


@Component


public class MyEventListener implements ApplicationListener<ApplicationEvent> {


    @Override

    public void onApplicationEvent(ApplicationEvent event) {
        // 在这里处理特定的事件逻辑
        System.out.println("收到应用程序事件:" + event.toString());
    }


}
package com.pany.camp.load; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; /** * * @description: ApplicationListener * @copyright: @Copyright (c) 2022 * @company: Aiocloud * @author: pany * @version: 1.0.0 * @createTime: 2023-07-06 22:09 */ @Component public class MyEventListener implements ApplicationListener<ApplicationEvent> { @Override public void onApplicationEvent(ApplicationEvent event) { // 在这里处理特定的事件逻辑 System.out.println("收到应用程序事件:" + event.toString()); } }

在上述示例中, MyEventListener 类实现了 ApplicationListener 接口,并指定了泛型参数为 ApplicationEvent ,表示监听所有类型的应用程序事件。您可以根据实际需求,将泛型参数指定为特定的事件类型。

当应用程序触发事件时, onApplicationEvent 方法会被调用,并传入相应的事件对象。您可以在该方法中编写处理事件的逻辑。

要注意的是,为了让 Spring Boot 自动注册 MyEventListener ,需要将其标记为 @Component 或使用其他方式将其纳入 Spring 容器的管理范围。

6、@EventListener 方式

@EventListener 注解可以用于监听 Spring 应用程序中的各种事件,包括启动事件。当应用程序启动时,带有 @EventListener 注解的方法会被自动触发。

使用代码如下:

package com.pany.camp.load;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
/**
*
* @description: @EventListener
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: pany
* @version: 1.0.0
* @createTime: 2023-07-06 22:11
*/
@Component
public class MyEventListenerExample {
@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
// 在这里添加应用程序启动时的逻辑
System.out.println(" @EventListener 应用程序已启动!" + event.toString());
}
}
package com.pany.camp.load;









import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;





/**


 *


 * @description:  @EventListener
 * @copyright: @Copyright (c) 2022 

 * @company: Aiocloud


 * @author: pany


 * @version: 1.0.0 

 * @createTime: 2023-07-06 22:11
 */


@Component


public class MyEventListenerExample {


    @EventListener
    public void handleContextRefresh(ContextRefreshedEvent event) {
        // 在这里添加应用程序启动时的逻辑
        System.out.println(" @EventListener 应用程序已启动!" + event.toString());
    }


}
package com.pany.camp.load; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; /** * * @description: @EventListener * @copyright: @Copyright (c) 2022 * @company: Aiocloud * @author: pany * @version: 1.0.0 * @createTime: 2023-07-06 22:11 */ @Component public class MyEventListenerExample { @EventListener public void handleContextRefresh(ContextRefreshedEvent event) { // 在这里添加应用程序启动时的逻辑 System.out.println(" @EventListener 应用程序已启动!" + event.toString()); } }

上述示例中, handleContextRefresh 方法被标记为 @EventListener ,并指定了 ContextRefreshedEvent 类型的事件。当应用程序启动并完成刷新时,该方法会被自动触发。

您可以在 handleContextRefresh 方法中添加应用程序启动时需要执行的逻辑。例如,您可以初始化一些数据、启动定时任务等。

请注意,为了让 Spring Boot 自动注册 MyApplication 类中的事件监听器,需要将其标记为 @SpringBootApplication 或使用其他方式将其纳入 Spring 容器的管理范围。

?? 本文由激流原创,原创不易,感谢支持
??喜欢的话记得点赞收藏啊
在这里插入图片描述

© 版权声明
THE END
喜欢就支持一下吧
点赞0

Warning: mysqli_query(): (HY000/3): Error writing file '/tmp/MYvElbqI' (Errcode: 28 - No space left on device) in /www/wwwroot/583.cn/wp-includes/class-wpdb.php on line 2345
admin的头像-五八三
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

图形验证码
取消
昵称代码图片