Spring消息與事件

          使用環境:MyEclipse6.0+JDK1.5+Spring 2.0 Core Libraries

          1,一個Java Bean
          package spring.main.message;

          public class User {
              
          private String name;
              
          private int age;
              
          public String getName() {
                  
          return name;
              }
              
          public void setName(String name) {
                  
          this.name = name;
              }
              
          public int getAge() {
                  
          return age;
              }
              
          public void setAge(int age) {
                  
          this.age = age;
              }
          }
          2,一個事件
          package spring.main.message;

          import org.springframework.context.ApplicationEvent;

          public class RainEvent extends ApplicationEvent {

              
          public RainEvent(Object source) {
                  
          super(source);
                  System.out.println(
          "烏云密布、閃電、打雷,緊接著,下起了瓢潑大雨。");
              }
              
          }
          3,一個事件監聽器
          package spring.main.message;

          import org.springframework.context.ApplicationEvent;
          import org.springframework.context.ApplicationListener;

          public class RainListener implements ApplicationListener{

              
          public void onApplicationEvent(ApplicationEvent arg0) {
                  
          if(arg0 instanceof RainEvent){
                      System.out.println(
          "唐僧大喊:"+arg0.getSource()+"趕緊收衣服咧!");
                  }
              }
          }
          4,二個資源文件:epan_en_US.properties
          userlogin user {0} login at {1}
             epan_zh_CN.properties
          userlogin \u4f7f\u7528\u8005 {0} \u4e8e {1}\u767b\u5f55
          這里說明下epan_zh_CN.properties的來源:
              4.1建文件test.properties,內容為:userlogin 使用者 {0} 于 {1}登錄;
              4.2在命令下行進入test.properties所在目錄,
              4.3使用命令:native2ascii test.properties epan_zh_CN.properties。
              4.4生成了epan_zh_CN.properties文件了。
          5,配置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="spring.main.message.User">
                  
          <property name="name">
                      
          <value>Epan</value>
                  
          </property>    
                  
          <property name="age">
                      
          <value>24</value>
                  
          </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="epan"></property>        
              
          </bean>

              
          <bean id="listener" 
                  
          class="spring.main.message.RainListener"></bean>
          </beans>

          6,一個測試
          package spring.main.message;

          import java.util.Calendar;
          import java.util.Locale;

          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;

          public class MainTest {

              
          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,"can't find the file",Locale.CHINA));
                  System.out.println(context.getMessage(
          "userlogin", obj,"can't find the file",Locale.US));
                  
                  context.publishEvent(
          new RainEvent("it's raining!"));
              }
          }
          7,建一個文件:log4j.properties
          log4j.rootLogger=WARN, stdout
          log4j.appender.stdout
          =org.apache.log4j.ConsoleAppender
          log4j.appender.stdout.layout
          =org.apache.log4j.PatternLayout
          log4j.appender.stdout.layout.ConversionPattern
          =%%p [%c] - %m%n
          8,運行MainTest,輸出如下結果:
          使用者 Epan 于 08-10-14 上午10:29登錄
          user Epan login at 
          10/14/08 10:29 AM
          烏云密布、閃電、打雷,緊接著,下起了瓢潑大雨。
          唐僧大喊:it
          's raining!趕緊收衣服咧!

          結構圖:



          posted on 2008-10-14 10:40 育平 閱讀(502) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2008年10月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 海兴县| 柳州市| 叙永县| 繁昌县| 临夏县| 宜都市| 荔浦县| 天水市| 庆云县| 马边| 满城县| 云林县| 北宁市| 蓬安县| 潮安县| 潞西市| 三穗县| 延川县| 乌拉特前旗| 朝阳县| 鄯善县| 苍溪县| 澎湖县| 临邑县| 教育| 武陟县| 朝阳市| 卢氏县| 剑河县| 红桥区| 汉源县| 达拉特旗| 纳雍县| 工布江达县| 改则县| 页游| 托克逊县| 环江| 抚宁县| 深泽县| 大名县|