Spring总结实例之消息与事件

出处:Examlink.com 作者:刀断水 日期:2008年07月08日 05时56分
  前几天看到网友总结的自学经验,觉得说得很好,引文:光看别人骑自行车很容易, 那么是不是看了几百遍别人怎么骑自行车你也就马上能骑着走了呢? 不摔跤是不可能学会的。

  还有就是要经常总结:刚才说到会摔跤, 那么这时候就要总结遇到的问题, 这样下次再遇到就不会再去回忆了. 好记性不如烂笔头. 注释, 如果今天不写, 那么以后只会越来越忙, 以后再也没时间写注释了. If you doesn't have time to do it today, then when do you have time to do it tomorrow?

  所以今天就写个Spring的消息和事件实例。

  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、用于国际化的两个消息资源文件: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("乌云密布、闪电、打雷,紧接着,下起了瓢泼大雨。");
 }
}

  下雨事件监听器: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()+"赶快收衣服喽!");
  }
 }
}

最后更新时间:2008-11-05 07:33:17
文章评论
共有 0 位网友发表了评论
用户名: 新注册) 密码: 匿名评论 [查看所有评论]

评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。

考试全流程