当前位置:   article > 正文

ApplicationContext.publishEvent()-异步调用

ApplicationContext.publishEvent()-异步调用

ApplicationContext.publishEvent()-异步调用

1.定义一个事件

//继承 ApplicationEvent

public class EventDemo extends ApplicationEvent {


    public EventDemo(String demand) {

        super(demand);

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

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() );

  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

1.定义一个事件

//继承 ApplicationEvent

public class EventDemo extends ApplicationEvent {


    public EventDemo(String demand) {

        super(demand);

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

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() );

  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

1.定义一个事件

//继承 ApplicationEvent

public class EventDemo extends ApplicationEvent {


    public EventDemo(String demand) {

        super(demand);

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

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() );

  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/153719
推荐阅读
相关标签
  

闽ICP备14008679号