Spring總結(jié)實(shí)例之消息與事件

字號(hào):

前幾天看到網(wǎng)友總結(jié)的自學(xué)經(jīng)驗(yàn),覺得說得很好,引文:光看別人騎自行車很容易, 那么是不是看了幾百遍別人怎么騎自行車你也就馬上能騎著走了呢? 不摔跤是不可能學(xué)會(huì)的。
    還有就是要經(jīng)??偨Y(jié):剛才說到會(huì)摔跤, 那么這時(shí)候就要總結(jié)遇到的問題, 這樣下次再遇到就不會(huì)再去回憶了. 好記性不如爛筆頭. 注釋, 如果今天不寫, 那么以后只會(huì)越來越忙, 以后再也沒時(shí)間寫注釋了. If you doesn't have time to do it today, then when do you have time to do it tomorrow?
    所以今天就寫個(gè)Spring的消息和事件實(shí)例。
    1、JavaBean:User.java
    package cn.xy.hw;
    /** *//**
    * @author hanwei
    *
    */
    public class User ...{
    private String name;
    private int age;
    public int getAge() ...{
    return age;
    }
    public void setAge(int age) ...{
    this.age = age;
    }
    public String getName() ...{
    return name;
    }
    public void setName(String name) ...{
    this.name = name;
    }
    }
    2、用于國際化的兩個(gè)消息資源文件:xiyou_en_US.properties和xiyou_zh_CN.properties
    userlogin user ...{0} login at ...{1}
    和
    userlogin 使用者 ...{0} 于 ...{1}登入
    自定義下雨的事件:RainEvent.java
    package cn.xy.hw;
    import org.springframework.context.ApplicationEvent;
    /** *//**
    * @author hanwei
    *
    */
    public class RainEvent extends ApplicationEvent ...{
    public RainEvent(Object arg0) ...{
    super(arg0);
    System.out.println("烏云密布、閃電、打雷,緊接著,下起了瓢潑大雨。");
    }
    }
    下雨事件監(jiān)聽器:RainListener.java
    package cn.xy.hw;
    import org.springframework.context.ApplicationEvent;
    import org.springframework.context.ApplicationListener;
    /** *//**
    * @author hanwei
    *
    */
    public class RainListener implements ApplicationListener ...{
    /**//* (non-Javadoc)
    * @see org.springframework.context.ApplicationListener#onApplicationEvent(
    org.springframework.context.ApplicationEvent)
    */
    public void onApplicationEvent(ApplicationEvent arg0) ...{
    if(arg0 instanceof RainEvent)...{
    System.out.println("唐僧大喊:"+arg0.getSource()+"趕快收衣服嘍!");
    }
    }
    }