[英]How to use PropertyChangeSupport and PropertyChangeListener?
@Controller
@RequestMapping("/scrape")
public class ScrapeController {
private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
public void addPropertyChangeListener(PropertyChangeListener listener) {
this.pcs.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
this.pcs.removePropertyChangeListener(listener);
}
@GetMapping({"/", ""})
public String scrape(){
this.pcs.firePropertyChange("value", "wait", "scrape"); // Breakpoint.
return "done";
}
}
public class ScrapeService implements PropertyChangeListener {
private final ScrapeController sc;
public ScrapeService(ScrapeController sc) {
this.sc = sc;
sc.addPropertyChangeListener(this);
}
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("Name = " + evt.getPropertyName()); // Breakpoint.
System.out.println("Old Value = " + evt.getOldValue());
System.out.println("New Value = " + evt.getNewValue());
}
}
This project is based on Spring.本项目基于Spring。 But I don't believe that the problem is Spring related.
但我不认为问题与 Spring 有关。
There are two breakpoints here.这里有两个断点。 The debugger stops at the first breakpoint in the
scrape
method where I fire the property change.调试器在我触发属性更改的
scrape
方法中的第一个断点处停止。 But the debugger fails to stop at the breakpoint in the propertyChange
method.但是调试器无法在
propertyChange
方法中的断点处停止。 So the debug messages are not being printed.所以调试消息没有被打印出来。
My guess is that the ScrapeService
is never actually instantiated and added to the ScrapeController
's addPropertyChangeListener
method.我的猜测是
ScrapeService
从未实际实例化并添加到ScrapeController
的addPropertyChangeListener
方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.