SpringBoot启动时加载指定方法的方式小结
作者:激流丶
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("应用程序启动时执行的方法");
}
}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("应用程序启动时执行的方法");
}
}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("应用程序启动时执行的方法");
}
}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());
}
}在上述示例中, 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());
}
}上述示例中, handleContextRefresh 方法被标记为 @EventListener ,并指定了 ContextRefreshedEvent 类型的事件。当应用程序启动并完成刷新时,该方法会被自动触发。
您可以在 handleContextRefresh 方法中添加应用程序启动时需要执行的逻辑。例如,您可以初始化一些数据、启动定时任务等。
请注意,为了让 Spring Boot 自动注册 MyApplication 类中的事件监听器,需要将其标记为 @SpringBootApplication 或使用其他方式将其纳入 Spring 容器的管理范围。
以上就是SpringBoot启动时加载指定方法的方式小结的详细内容,更多关于SpringBoot加载指定方法的资料请关注脚本之家其它相关文章!
