• Spring ApplicationContext中的”事件传递“笔记


    ApplicationContext中的事件处理是通过ApplicationEvent类和ApplicationListener接口来提供。通过ApplicationContext的publishEvent()方法来通知ApplicationListener。

    下面是一个简单的测试例子。。。

    第一步、创建一个类LogEvent用来继承ApplicationEvent

    package spring.init.log;
    
    import org.springframework.context.ApplicationEvent;
    
    /**
     * @author fanbo
     * ApplicationContext事件传递Log例子
     * 继承org.springframework.context.ApplicationEvent;
     */
    public class LogEvent extends ApplicationEvent{
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        
        public LogEvent(Object msg) {
            super(msg);
            // TODO Auto-generated constructor stub
            System.out.println("LOGEVENT传过来的值"+msg);
        }
    }

    第二步、创建一个类LogListener用来实现ApplicationListener接口。

    package spring.init.log;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.springframework.context.ApplicationEvent;
    import org.springframework.context.ApplicationListener;
    
    /**
     * @author fanbo
     * ApplicationContext事件传递Log例子
     * 实现org.springframework.context.ApplicationListener;接口
     */
    public class LogListener implements ApplicationListener{
    
        /*
         * 实现onApplicationEvent(non-Javadoc)
         * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
         */
        public void onApplicationEvent(ApplicationEvent event) {
            // TODO Auto-generated method stub
            //如果传递过来的参数是LogEvent的实例,
            if(event instanceof LogEvent){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String nowdate = sdf.format(new Date());
                System.out.println("当前时间是:"+nowdate);
            }
        }
    
    }

    第三步、创建一个类Log用来实现ApplicationContextAware接口

    package spring.init.log;
    
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    
    /**
     * @author fanbo
     *  ApplicationContext事件传递Log例子
     *  实现org.springframework.context.ApplicationContextAware;接口
     */
    public class Log implements ApplicationContextAware{
        
        private ApplicationContext applicationContext;
    
        /*
         * 实现接口中的方法
         */
        public void setApplicationContext(ApplicationContext ac)
                throws BeansException {
            // TODO Auto-generated method stub
            this.applicationContext = ac;
            
        }
        
        /**
         * 通过ApplicationContext的publishEvent方法来传递给ApplicationListener
         * @param log
         * @return
         */
        public int LogInfo(String log){
            
            LogEvent le = new LogEvent(log);
            //通过ApplicationContext的publishEvent方法来传递给ApplicationListener
            this.applicationContext.publishEvent(le);
            return 1;
        }
        
    
    }

    第四步、配置Bean.xml文件

        <!-- ApplicationCOntext事件传递 -->
        <bean id="log" class="spring.init.log.Log" />
        <bean id="listener" class="spring.init.log.LogListener"></bean>
        

    第五步、测试

    package spring.init.log;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    public class LogTest {
    
        public static void main(String[] args) {
            ApplicationContext ac = new FileSystemXmlApplicationContext("conf/xml/config.xml");
            Log log = (Log) ac.getBean("log");
            log.LogInfo("gf");
        }
    }

    输出结果

    log4j:WARN No such property [maxFileSize] in org.apache.log4j.ConsoleAppender.
    log4j:WARN No such property [file] in org.apache.log4j.ConsoleAppender.
    log4j:WARN No such property [append] in org.apache.log4j.ConsoleAppender.
    log4j:WARN No such property [maxBackupIndex] in org.apache.log4j.ConsoleAppender.
    LOGEVENT传过来的值gf
    当前时间是:2013-12-03 13:50:44
    此笔记用来自我学习和分享知识,有不对的地方还请大家互相指教
  • 相关阅读:
    YTU 2972: C语言习题5.24--文件操作1
    YTU 2925: 文件操作--文本文件读入
    YTU 2924: 文件操作--二进制文件读入
    PHP中$_SERVER[HTTP_REFERER]
    form控件中添加js代码,用javascript:某代码段(注意javascript之后用双引号)
    js中的location.href与location
    问题:下载页面代码? 以及php中header的用法。
    随笔
    __FILE__ $_SERVER['PHP_SELF'] $_SERVER['SCRIPT_NAME'] $_SERVER['SCRIPT_FILENAME'] 的区别
    #deebef 背景色
  • 原文地址:https://www.cnblogs.com/willbesuccess/p/3455694.html
Copyright © 2020-2023  润新知