First, define a base class, responsible for joining the listener,
Package net.cafe;
Import java.beans.propertychangesupport; import java.beans.propertychangelistener;
/ ** * Created by Intellij Idea. * User: Jjyao * Date: 2004-11-1 * Time: 23:01:56 * To change this template Use file | settings | file templates. * / Public class baseeventbean {
Protected PropertyChangeSupport PCS;
Public void addpropertyChangeListener (PCS == NULL) {PCS = New PropertyChangeSupport (this);} pcs.addpropertyChangeListener (Listener);
Public Void RemoveChangelistener {if (pcs! = null) {pcs.removepropertychangelistener (listener);}}
}
A POJO inherits this class and adds a trigger event to the SET method.
Package net.cafe;
/ ** * Created by Intellij IDEA. * User: Jjyao * Date: 2004-11-1 * Time: 23:15:34 * To change this template Use file | settings | file templates. * / Public class foolbean extends baseeventbean {
Private string username; private string password;
Public string getpassword () {return password;}
Public void setPassword (String newpassword) {string oldpassword = password; this.password = newpassword; pcs.firepropertychange ("password", oldpassword, password);}
Public string getUsername () {return username;}
Public void setusername (string username) {this.username = usrname;}
}
Attribute listener
Package net.cafe;
Import java.beans.propertychangelistener; import java.beans.propertychangeevent;
/ ** * Created by IntelliJ IDEA * User: JJYAO * Date: 2004-11-1 * Time:. 23:12:33 * To change this template use File | Settings | File Templates * / public class Listener implements PropertyChangeListener {. Public void PropertyChange (PropertyChangeeevent EVT) {system.out.println ("=========== =============" evt.getoldValue ()); system.out.println ("=========== New value ========" est.getnewvalue ());}}
Test code
Package net.cafe;
/ ** * Created by Intellij IDEA. * User: Jjyao * Date: 2004-11-1 * Time: 23:24:28 * Time: 23:24:28 * To change this Template Use File | Settings | File Templates. * / Public Class Beaneventtest {
Public static void main (string [] args) {foolbean bean = new foolbean (); bean.addpropertychangelistener (new listener ()); bean.setpassword ("22222"); bean.setpassword ("33333");}}
BTW, Sun's Java.Beans. * Contains most space packages, these methods need to scale themselves, we don't need to use it to define the method name
The above example can be implemented using your own method,