Home > Enterprise >  How to use PropertyChangeSupport and PropertyChangeListener?
How to use PropertyChangeSupport and PropertyChangeListener?

Time:12-17

@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.

Could you help me?

CodePudding user response:

My guess is that the ScrapeService is never actually instanciated and added to the ScrapeController's addPropertyChangeListener method.

  • Related