赞
踩
//继承 ApplicationEvent
public class EventDemo extends ApplicationEvent {
public EventDemo(String demand) {
super(demand);
}
}
// 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() );
}
}
@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; } }
// 测试时 记得启动类上添加 @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); } }
//继承 ApplicationEvent
public class EventDemo extends ApplicationEvent {
public EventDemo(String demand) {
super(demand);
}
}
// 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() );
}
}
@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; } }
// 测试时 记得启动类上添加 @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); } }
//继承 ApplicationEvent
public class EventDemo extends ApplicationEvent {
public EventDemo(String demand) {
super(demand);
}
}
// 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() );
}
}
@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; } }
// 测试时 记得启动类上添加 @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); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。