springboot 服务启动后执行如何执行特定方法

1. CommandLineRunner  2.ApplicationRunner 3. 事件注册(监听 spring中的默认一些事件)

代码如下:

@Order(2)
@Component
public class ApplicationRunnerImpl1 implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        System.out.println("服务启动后执行的操作----------ApplicationRunnerImpl1----order  2------start------");
        if (null != applicationArguments) {
            for ( String s:applicationArguments.getNonOptionArgs()){
                System.out.println("服务启动后执行的操作----------ApplicationRunnerImpl1--order  2----"+s);
            }
        }

        System.out.println("服务启动后执行的操作----------ApplicationRunnerImpl1------order  2-----start------");

    }
}
@Order(4)
@Component
public class ApplicationRunnerImpl2 implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        System.out.println("服务启动后执行的操作----------ApplicationRunnerImpl2-----order  4-----start------");
        if (null != applicationArguments) {
            for ( String s:applicationArguments.getNonOptionArgs()){
                System.out.println("服务启动后执行的操作----------ApplicationRunnerImpl2---rder  4--"+s);
            }
        }
        System.out.println("服务启动后执行的操作----------ApplicationRunnerImpl2------rder  4----start------");

    }
}
@Order(1)
@Component
public class CommandLineRunnerImpl1 implements CommandLineRunner {
    @Override
    public void run(String... strings) throws Exception {

        System.out.println("服务启动后执行的操作----------CommandLineRunnerImpl1-----order  1------start------");
        if (null != strings) {
            for ( String s:strings){
                System.out.println("服务启动后执行的操作----------CommandLineRunnerImpl1--order  1---"+s);
            }
        }

        System.out.println("服务启动后执行的操作----------CommandLineRunnerImpl1-------order  1---start------");


    }
}
@Order(3)
@Component
public class CommandLineRunnerImpl2 implements CommandLineRunner {
    @Override
    public void run(String... strings) throws Exception {

        System.out.println("服务启动后执行的操作----------CommandLineRunnerImpl2------order  3-----start------");
        if (null != strings) {
            for ( String s:strings){
                System.out.println("服务启动后执行的操作----------CommandLineRunnerImpl2--order  3----"+s);
            }
        }
        System.out.println("服务启动后执行的操作----------CommandLineRunnerImpl2--------order  3---start------");

    }
}
@Configuration
public class EventListenerRunner2  {

    @EventListener(classes = {SpringApplicationEvent.class})
    public void listen(SpringApplicationEvent event) {
        System.out.println(" 服务启动后 注解事件触发:" + event.getClass().getName());
    }
}

注意:下面这个写法没有生效

@Component
public class EventListenerRunner1  implements ApplicationListener<ApplicationStartedEvent> {

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        System.out.println(" 服务启动后 监听检测到了 EventListenerRunner1");
    }
}
服务启动后执行的操作----------CommandLineRunnerImpl1-----order  1------start------
服务启动后执行的操作----------CommandLineRunnerImpl1-------order  1---start------
服务启动后执行的操作----------ApplicationRunnerImpl1----order  2------start------
服务启动后执行的操作----------ApplicationRunnerImpl1------order  2-----start------
服务启动后执行的操作----------CommandLineRunnerImpl2------order  3-----start------
服务启动后执行的操作----------CommandLineRunnerImpl2--------order  3---start------
服务启动后执行的操作----------ApplicationRunnerImpl2-----order  4-----start------
服务启动后执行的操作----------ApplicationRunnerImpl2------rder  4----start------
 服务启动后 注解事件触发:org.springframework.boot.context.event.ApplicationReadyEvent

springboot  中 事件监听的用法:

①自定义事件  ② 监听事件 ③ 发布事件

自定义事件


import org.springframework.context.ApplicationEvent;

/**
 * @program: springboot_01
 * @description:  继承  ApplicationEvent  自定义事件
 * @author: guoyiguang
 * @create: 2021-06-02 11:51
 **/
public class CustomEvent extends  ApplicationEvent  {

    
    /** 
    * 要封装的消息,可以是自定义对象
    */ 
    private String message;
    public CustomEvent(Object source,String message) {
        super(source);
        this.message = message;
    }

    /**
     * 调用 getMessage 获取 被封装的消息
     */
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

② 监听事件


import com.example.demo.entity.event.CustomEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @program: springboot_01
 * @description: 事件监听
 * @author: guoyiguang
 * @create: 2021-06-02 11:21
 **/
@Component
public class EventListenerRunner3 {

    @EventListener(CustomEvent.class)
    public void onApplicationEvent(CustomEvent customEvent){
        System.out.println("接收到的消息为:"+customEvent.getMessage());
    }
}

③ 触发事件

/**
 * @program: springboot_01
 * @description:
 * @author: guoyiguang
 * @create: 2021-06-02 11:56
 **/

@RestController
public class EventController {

    @Resource
    private ApplicationContext applicationContext;

    @RequestMapping("/hello")
    public String hello(){
        System.out.println("事件开始发布消息:"+System.currentTimeMillis());
        applicationContext.publishEvent(new CustomEvent(this,"嘿嘿嘿!"));
        return "success";
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值