服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

spring总结实例之消息与事件

本文转自javaeye,文中内容不代表本站观点,仅提供参考。

  前几天看到网友总结的自学经验,觉得说得很好,引文:光看别人骑自行车很容易, 那么是不是看了几百遍别人怎么骑自行车你也就马上能骑着走了呢? 不摔跤是不可能学会的。

  还有就是要经常总结:刚才说到会摔跤, 那么这时候就要总结遇到的问题, 这样下次再遇到就不会再去回忆了. 好记性不如烂笔头. 注释, 如果今天不写, 那么以后只会越来越忙, 以后再也没时间写注释了. 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()+"赶快收衣服喽!");
  }
 }
}

  配置文件:applicationcontext.xml

<!--sp-->xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="user" class="cn.xy.hw.user" abstract="false"
lazy-init="default" autowire="default" dependency-check="default">
<property name="name">
 <value>hanweivalue>
  property>
 <property name="age">
 <value>20value>
  property>
 bean>

 <bean id="messagesource"
class="org.springframework.context.support.resourcebundlemessagesource"
abstract="false" lazy-init="default" autowire="default"
dependency-check="default">
<property name="basename" value="xiyou">property>
bean>

<bean id="listener" class="cn.xy.hw.rainlistener" abstract="false"
lazy-init="default" autowire="default" dependency-check="default">
bean>

beans>

  测试类:miantest.java

package cn.xy.hw;

import java.util.calendar;
import java.util.locale;

import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;

/** *//**
* @author hanwei
*
*/
public class miantest ...{

 public static void main(string[] args) ...{
  applicationcontext context = new classpathxmlapplicationcontext("applicationcontext.xml");
  user user = (user)context.getbean("user");

  object[] obj=new object[]...{user.getname(),calendar.getinstance().gettime()};
system.out.println(context.getmessage("userlogin",obj
,"找不到指定模块!",locale.china));
  system.out.println(context.getmessage("userlogin",obj
,"找不到指定模块!",locale.us));

  context.publishevent(new rainevent("下雨了!"));
 }
}

  ok了,这是运行测试类的结果:

使用者 hanwei 于 07-8-26 下午6:14登入
user hanwei login at 8/26/07 6:14 pm
乌云密布、闪电、打雷,紧接着,下起了瓢泼大雨。
唐僧大喊:下雨了!赶快收衣服喽!
log4j:warn no appenders could be found for logger (org.springframework.context.support.classpathxmlapplicationcontext).
log4j:warn please initialize the log4j system properly.

扫描关注微信公众号