事件对象
package com.wzq.demoftl.observer;
import org.springframework.context.ApplicationEvent;
public class ObserverEvent extends ApplicationEvent {
private String name;
/**
* Create a new {@code ApplicationEvent}.
*
* @param source the object on which the event initially occurred or with
* which the event is associated (never {@code null})
*/
public ObserverEvent(Object source, String name) {
super(source);
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
监听事件
package com.wzq.demoftl.observer;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class ObserverListener implements ApplicationListener<ObserverEvent> {
@Override
public void onApplicationEvent(ObserverEvent event) {
System.out.println("event = " + event.getName());
}
}
发布
@Autowired
private ApplicationContext applicationContext;
@RequestMapping("testL")
public void testListener(){
this.applicationContext.publishEvent(new ObserverEvent(this, "吴志奇"));
}