Spring总结实例之消息 事件??? 今天看了美河夜色狼族的一集Spring视频,是关于Bean的高级部分:消息和事件。
Spring总结实例之消息 事件
??? 今天看了美河夜色狼族的一集Spring视频,是关于Bean的高级部分:消息和事件。以前看书或看视频,只是看了却没有动手编代码,看后就忘了。
??? 前几天看到网友总结的自学经验,觉得说得很好,引文:光看别人骑自行车很容易, 那么是不是看了几百遍别人怎么骑自行车你也就马上能骑着走了呢? 不摔跤是不可能学会的。
??? 还有就是要经常总结:刚才说到会摔跤, 那么这时候就要总结遇到的问题, 这样下次再遇到就不会再去回忆了. 好记性不如烂笔头. 注释, 如果今天不写, 那么以后只会越来越忙, 以后再也没时间写注释了. 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

<!---->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.
?
1 楼 lizhaosuper 2007-08-27 可以把讲spring的是视频给共享一下不 2 楼 hanwei59 2007-08-27 这有美河的ftp地址,自己下吧。
FTP地址:60.190.223.247
FTP帐号:eimhe.com
FTP密码:原创
ftp://eimhe.com:原创@60.190.223.247/
下的部分目录结构:
├─[美河原创作品]J2EE系列讲座之Hibernate
│hibernate1.rar
│hibernate2.1.rar
│hibernate2.2.rar
│hibernate2.3.rar
├─[美河原创作品]J2EE系列讲座之SPRING
│spring1.rar
│spring2.rar
│spring3.rar
│spring3a.rar
│spring3b.rar
├─[美河原创作品]J2EE系列讲座之SSH
│ssh1.rar
│ssh2.1.rar
│ssh2.2.rar
├─[美河原创作品]J2EE系列讲座之STRUTS
│struts1.rar
│struts2.rar
│struts3.1.rar
│struts3.2.rar
│struts3.rar
│struts4.rar
如:
ftp://eimhe.com:原创@60.190.223.247/[美河原创作品]J2EE系列讲座之SPRING/spring1.rar