ApplicationContext.publishEvent()-异步调用

该文章展示了如何在SpringBoot应用中创建自定义事件,注册监听器以及通过ApplicationContext的publishEvent()方法异步发布事件。测试时需启用@EnableAsync注解以实现异步调用。

ApplicationContext.publishEvent()-异步调用

1.定义一个事件

//继承 ApplicationEvent

public class EventDemo extends ApplicationEvent {


    public EventDemo(String demand) {

        super(demand);

    }

}

2.注册一个监听器

// 1.继承监听器接口
public interface EventServiceDemo extends ApplicationListener<EventDemo> {
}

//2. 实现类实现接口,重写方法 添加业务代码
@Service
public class EventServiceDemoImpl implements EventServiceDemo {

  @Override
  public void onApplicationEvent(EventDemo event) {

    System.out.println(event.getSource() );

  }
}

3. 使用


@RestController
@RequestMapping(value = "/home")
public class HelloController {
    // 注入SpringBoot容器
    @Autowired
    private ApplicationContext context;

    @RequestMapping("/event/{str}")
    public String event( @PathVariable String str ){
        // 容器推送一个事件, 推送自定义事件
        context.publishEvent(new EventDemo("我触发了一个事件"));

        return str;
    }
}

4.测试

// 测试时 记得启动类上添加 @EnableAsync 启动异步调用
@SpringBootApplication
@EnableAsync // 启动异步调用
public class SpringBootPublishEventApplication  extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        application.application().setBannerMode(Banner.Mode.OFF);
        return application.sources(SpringBootPublishEventApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(SpringBootPublishEventApplication.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
    }
}

1.定义一个事件

//继承 ApplicationEvent

public class EventDemo extends ApplicationEvent {


    public EventDemo(String demand) {

        super(demand);

    }

}

2.注册一个监听器

// 1.继承监听器接口
public interface EventServiceDemo extends ApplicationListener<EventDemo> {
}

//2. 实现类实现接口,重写方法 添加业务代码
@Service
public class EventServiceDemoImpl implements EventServiceDemo {

  @Override
  public void onApplicationEvent(EventDemo event) {

    System.out.println(event.getSource() );

  }
}

3. 使用


@RestController
@RequestMapping(value = "/home")
public class HelloController {
    // 注入SpringBoot容器
    @Autowired
    private ApplicationContext context;

    @RequestMapping("/event/{str}")
    public String event( @PathVariable String str ){
        // 容器推送一个事件, 推送自定义事件
        context.publishEvent(new EventDemo("我触发了一个事件"));

        return str;
    }
}

4.测试

// 测试时 记得启动类上添加 @EnableAsync 启动异步调用
@SpringBootApplication
@EnableAsync // 启动异步调用
public class SpringBootPublishEventApplication  extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        application.application().setBannerMode(Banner.Mode.OFF);
        return application.sources(SpringBootPublishEventApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(SpringBootPublishEventApplication.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
    }
}

1.定义一个事件

//继承 ApplicationEvent

public class EventDemo extends ApplicationEvent {


    public EventDemo(String demand) {

        super(demand);

    }

}

2.注册一个监听器

// 1.继承监听器接口
public interface EventServiceDemo extends ApplicationListener<EventDemo> {
}

//2. 实现类实现接口,重写方法 添加业务代码
@Service
public class EventServiceDemoImpl implements EventServiceDemo {

  @Override
  public void onApplicationEvent(EventDemo event) {

    System.out.println(event.getSource() );

  }
}

3. 使用


@RestController
@RequestMapping(value = "/home")
public class HelloController {
    // 注入SpringBoot容器
    @Autowired
    private ApplicationContext context;

    @RequestMapping("/event/{str}")
    public String event( @PathVariable String str ){
        // 容器推送一个事件, 推送自定义事件
        context.publishEvent(new EventDemo("我触发了一个事件"));

        return str;
    }
}

4.测试

// 测试时 记得启动类上添加 @EnableAsync 启动异步调用
@SpringBootApplication
@EnableAsync // 启动异步调用
public class SpringBootPublishEventApplication  extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        application.application().setBannerMode(Banner.Mode.OFF);
        return application.sources(SpringBootPublishEventApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(SpringBootPublishEventApplication.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值