国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

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

2019-11-17 05:54:58
字體:
供稿:網(wǎng)友

  前幾天看到網(wǎng)友總結(jié)的自學(xué)經(jīng)驗,覺得說得很好,引文:光看別人騎自行車很輕易, 那么是不是看了幾百遍別人怎么騎自行車你也就馬上能騎著走了呢? 不摔跤是不可能學(xué)會的。

  還有就是要經(jīng)常總結(jié):剛才說到會摔跤, 那么這時候就要總結(jié)碰到的問題, 這樣下次再碰到就不會再去回憶了. 好記性不如爛筆頭. 注釋, 假如今天不寫, 那么以后只會越來越忙, 以后再也沒時間寫注釋了. 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("烏云密布、閃電、打雷,緊接著,下起了瓢潑大雨。");
 }
}

  下雨事件監(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()+"趕緊收衣服嘍!");
  }
 }
}

  配置文件: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了,這是運行測試類的結(jié)果:

使用者 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. 



發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宝清县| 葫芦岛市| 余江县| 灵宝市| 环江| 吉首市| 赤峰市| 达尔| 隆回县| 中西区| 左云县| 溧水县| 眉山市| 巴青县| 汝南县| 光山县| 阿拉善盟| 深泽县| 五大连池市| 长海县| 丰都县| 永康市| 旬阳县| 扎囊县| 澳门| 彰化县| 裕民县| 沈阳市| 育儿| 依兰县| 泗阳县| 通榆县| 舒兰市| 鞍山市| 綦江县| 竹北市| 林口县| 西平县| 色达县| 柳林县| 轮台县|