熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Spring總結實例之消息與事件

2022-06-13   來源: Java開源技術 

  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


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28987.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.