java

位置:IT落伍者 >> java >> 浏览文章

Spring总结实例之消息与事件


发布日期:2022年10月08日
 
Spring总结实例之消息与事件

Spring的消息和事件实例

JavaBeanUserjava

package cnxyhw;

/** *//**

* @author hanwei

*

*/

public class User {

private String name;

private int age;

public int getAge() {

return age;

}

public void setAge(int age) {

thisage = age;

}

public String getName() {

return name;

}

public void setName(String name) {

thisname = name;

}

}

用于国际化的两个消息资源文件xiyou_en_USproperties和xiyou_zh_CNproperties

userlogin user {} login at {}和userlogin 使用者 {} 于 {}登入

自定义下雨的事件RainEventjava

package cnxyhw;

import orgspringframeworkcontextApplicationEvent;

/** *//**

* @author hanwei

*

*/

public class RainEvent extends ApplicationEvent {

public RainEvent(Object arg) {

super(arg);

Systemoutprintln(乌云密布闪电打雷紧接着下起了瓢泼大雨);

}

}

下雨事件监听器RainListenerjava

package cnxyhw;

import orgspringframeworkcontextApplicationEvent;

import orgspringframeworkcontextApplicationListener;

/** *//**

* @author hanwei

*

*/

public class RainListener implements ApplicationListener {

/**//* (nonJavadoc)

* @see orgspringframeworkcontextApplicationListener#onApplicationEvent(

orgspringframeworkcontextApplicationEvent)

*/

public void onApplicationEvent(ApplicationEvent arg) {

if(arg instanceof RainEvent){

Systemoutprintln(唐僧大喊+arggetSource()+赶快收衣服喽!);

}

}

}

配置文件applicationContextxml

<!sp>xml version= encoding=UTF?>

<beans xmlns=

xmlns:xsi=

xsi:schemaLocation=

<bean id=user class=cnxyhwUser abstract=false

lazyinit=default autowire=default dependencycheck=default

<property name=name

<value>hanweivalue>

property>

<property name=age

<value>value>

property>

bean>

<bean id=messageSource

class=orgspringframeworkcontextsupportResourceBundleMessageSource

abstract=false lazyinit=default autowire=default

dependencycheck=default

<property name=basename value=xiyou>property>

bean>

<bean id=listener class=cnxyhwRainListener abstract=false

lazyinit=default autowire=default dependencycheck=default

bean>

beans>

测试类MianTestjava

package cnxyhw;

import javautilCalendar;

import javautilLocale;

import orgspringframeworkcontextApplicationContext;

import orgspringframeworkcontextsupportClassPathXmlApplicationContext;

/** *//**

* @author hanwei

*

*/

public class MianTest {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext(applicationContextxml);

User user = (User)contextgetBean(user);

Object[] obj=new Object[]{usergetName()CalendargetInstance()getTime()};

Systemoutprintln(contextgetMessage(userloginobj

找不到指定模块!LocaleCHINA));

Systemoutprintln(contextgetMessage(userloginobj

找不到指定模块!LocaleUS));

contextpublishEvent(new RainEvent(下雨了!));

}

}

OK了这是运行测试类的结果

使用者 hanwei 于 下午:登入

user hanwei login at // : PM

乌云密布闪电打雷紧接着下起了瓢泼大雨

唐僧大喊下雨了!赶快收衣服喽!

logj:WARN No appenders could be found for logger (orgspringframeworkcontextsupportClassPathXmlApplicationContext)

logj:WARN Please initialize the logj system properly

               

上一篇:基于Eclipse的工具开发: 设定构建路径实战[2]

下一篇:开发Eclipse下的自定义控件[1]