崛起于Springboot2.X+事件监听器(71)
《SpringBoot2.X心法总纲》
事件主题:认识一个女孩之后,触发事件监听,得到结果:认识到了一个女孩
1、配置
@Componentpublic class Girl {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Componentpublic class UnderstandEvent extends ApplicationEvent {
public UnderstandEvent(Girl source) {
super(source);
}
public Girl know(){
return (Girl) getSource();
}
}
2、事件发布
@Servicepublic class GirlService {
@Autowired
ApplicationEventPublisher applicationEventPublisher;
public void knowGirl(Girl girl){
applicationEventPublisher.publishEvent(new UnderstandEvent(girl));
}
@EventListener
public void afterKnow(UnderstandEvent understandEvent){
Girl girl = understandEvent.know();
System.out.println("认识了一个新女孩,她的名字叫" + girl.getName());
}
}
3、contoller层
@AutowiredGirlService girlService;
@GetMapping(value = "/know")
public void know(){
Girl girl = new Girl();
girl.setId("12323");
girl.setName("杨幂");
girlService.knowGirl(girl);
}
4、测试结果
以上是 崛起于Springboot2.X+事件监听器(71) 的全部内容, 来源链接: utcz.com/z/512335.html