博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring中的事件传播
阅读量:2230 次
发布时间:2019-05-09

本文共 1097 字,大约阅读时间需要 3 分钟。

package com.logcd.beans;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class LoginAction implements ApplicationContextAware{
    private ApplicationContext applicationContext;
   
    public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
    this.applicationContext = applicationContext;
    }
   
    public int login(String username) {
        ActionEvent event = new ActionEvent(this,username+"在登录!");
        this.applicationContext.publishEvent(event);
        return 0;
    }
}
(3)listener类:
package com.logcd.beans;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
public class ActionListener implements ApplicationListener{
    public void onApplicationEvent(ApplicationEvent event) {
            //只处理自己应该处理的
           if (event instanceof ActionEvent) {
            System.out.println(((ActionEvent)event).getMessage());
            }
    }
}
 
(4)配置
    <bean id="loginAction" class="com.logcd.beans.LoginAction"/>
    <bean id="listener" class="com.logcd.beans.ActionListener"/>

 

转载地址:http://fsgfb.baihongyu.com/

你可能感兴趣的文章
Git上传代码时碰到的问题及解决方法
查看>>
【Linux】vim的简单配置
查看>>
【C++】智能指针
查看>>
【C++】const修饰的成员函数
查看>>
【C++】面向对象的三大特性
查看>>
【C++】智能指针(后续)
查看>>
【C】堆区和栈区的区别
查看>>
【linux】send和recv函数解析
查看>>
【Linux】线程安全的单例模式以及计算密集型线程和IO密集型线程
查看>>
一次完整的HTTP请求是怎样的??
查看>>
【C++】常见的内存泄漏及解决方法
查看>>
【C++】const 指针与指向const的指针
查看>>
【Linux】多线程和多进程 及其应用场景
查看>>
【C++】构造函数中必须通过初始化列表来进行初始化情况
查看>>
【算法】对于大数的操作
查看>>
【操作系统】系统调用的概念
查看>>
【计算机网络】cookie和session的区别
查看>>
【C++】构造函数、析构函数抛出异常的问题
查看>>
【C++】关于vector<bool>
查看>>
【操作系统】内存碎片产生原因及终极解决办法
查看>>