简体   繁体   中英

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. But I don't believe that the problem is Spring related.

There are two breakpoints here. The debugger stops at the first breakpoint in the scrape method where I fire the property change. But the debugger fails to stop at the breakpoint in the propertyChange method. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM