<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 育平 閱讀(501) 評論(0)  編輯  收藏


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


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

    導航

    統計

    常用鏈接

    留言簿(2)

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲精品在线不卡| 亚洲毛片免费观看| 四虎永久在线精品免费一区二区| 永久免费av无码不卡在线观看| 久久精品国产亚洲av高清漫画 | 亚洲永久精品ww47| 成年大片免费视频播放一级| 亚洲国产婷婷综合在线精品| 亚欧国产一级在线免费| 亚洲午夜爱爱香蕉片| 久久精品免费网站网| 亚洲乱码精品久久久久..| 日韩电影免费在线观看网站 | 日本媚薬痉挛在线观看免费| 亚洲丶国产丶欧美一区二区三区| 四虎在线免费播放| 老湿机一区午夜精品免费福利| yy6080亚洲一级理论| 国产免费久久精品99久久| 久久精品国产精品亚洲| 在线观看免费无码专区| 亚洲综合激情另类小说区| 成全视频免费高清| 黄色免费在线网址| 亚洲av综合avav中文| 免费做爰猛烈吃奶摸视频在线观看| 亚洲a∨国产av综合av下载| 亚洲精品美女久久久久99小说| 国产免费拔擦拔擦8X高清在线人| 日产亚洲一区二区三区| 成人激情免费视频| 一个人免费播放在线视频看片| 亚洲成AV人片一区二区| 无码一区二区三区AV免费| 精品免费AV一区二区三区| 精品国产综合成人亚洲区 | 免费无码黄动漫在线观看| 免费人成网上在线观看| 亚洲国产精品一区二区久久| 午夜a级成人免费毛片| 怡红院免费全部视频在线视频|