Import java.beans.PropertyChangeEvent;
Import Java.beans.PropertyChangeListener;
Import Java.beans.PropertyChangeSupport;
public class Propertychangesupporttest {static Testbean Testbean = new Testbean (); public static void Main (string[] args) {Propertychangesupport propertychangesupport = new Propertychangesupport (testbea
n); Propertychangesupport.addpropertychangelistener (New PropertyChangeListener () {@Override public void PropertyChange ( Propertychangeevent evt) {if (Evt.getpropertyname (). Equals ("name")) {System.out.println ("bean:" + propertychanges UpportTest.this.testBean + "field:" + evt.getpropertyname () + "value Change,old value:" + evt.getoldvalue () + "New Valu
E: "+ evt.getnewvalue ());
}
}
});
Testbean.setid (111);
Testbean.setname ("Mike"); Propertychangesupport.firepropertychange ("name", "", "Mike");
Notifies the property that the value has been modified to notify the relevant listener for processing} public static class testbean{private int id;
private String name;
public int getId () { return ID;
The public void setId (int id) {this.id = ID;
Public String GetName () {return name;
public void SetName (String name) {this.name = name; }
}
}
This is the JDK application scenario for the Observer model, and in our usual projects, for example, in order to achieve dynamic loading of application configurations, we need to notify the application points that apply this parameter to make the corresponding changes when applying the configuration parameters that are applied dynamically after startup. Then you can use this propertychangesupport to do it.