spring学习记录(五)

spring监听器
说起spring,一般都会说到IOC和AOP两个最重要的基石,但是除了IOC和AOP,spring框架中还有Environment和多播器ApplicationEventMulticaster。
其中,Environment是spring容器存储java等环境变量的结构;而多播器则是发布事件用的结构。
ApplicationEventMulticaster作为abstractApplicationContext的一个属性,在refresh方法中通过调用如下子方法进行初始化:

// Initialize event multicaster for this context.
initApplicationEventMulticaster();

abstractApplicationContext中默认装载的是SimpleApplicationEventMulticaster的对象,对象中通过如下方法进行事件的发布–multicast

public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
		ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
		Executor executor = getTaskExecutor();
		for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
			if (executor != null) {
				executor.execute(() -> invokeListener(listener, event));
			}
			else {
				invokeListener(listener, event);
			}
		}
	}

上面的方法内可以看出,如果IOC容器中存在线程池,则会通过异步的方式来进行相应监听器的调用,否则直接通过同步的方式来进行监听器的调用。
在spring中进行监听模型的使用,则需要做如下处理:
1、定义相应的event,通过继承ApplicationEvent的方式;
2、定义相应的Listener,通过实现ApplicationListener的方式;
3、在需要发布相应事件的类中,实现ApplicationEventPublisherAware接口,实现ApplicationEventPublisher组件的导入,通过ApplicationEventPublisher组件的publishEvent方法来进行相应事件的发布。
在以上第三步中,经过跟踪,其实ApplicationEventPublisher即是IOC容器本身,也就是ApplicationContext,所以,如果需要发布事件的类实现ApplicationContextAware接口导入IOC容器,也可以通过ApplicationContext的publishEvent方法来进行event的发布。
具体代码如下:

import org.springframework.context.ApplicationEvent;

public class OrderSuccessEvent extends ApplicationEvent {
    /**
     * Create a new {@code ApplicationEvent}.
     *
     * @param source the object on which the event initially occurred or with
     *               which the event is associated (never {@code null})
     */
    public OrderSuccessEvent(Object source) {
        super(source);
    }
}


import com.huwc.bean.Order;
import com.huwc.event.OrderSuccessEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class OrderSuccessListener implements ApplicationListener<OrderSuccessEvent> {
    public void onApplicationEvent(OrderSuccessEvent event) {
        Order order = (Order) event.getSource();

        System.out.println("监听到orderSuccessEvent:order:" + order);
    }
}




import com.huwc.bean.Order;
import com.huwc.event.OrderSuccessEvent;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderSerivce, ApplicationEventPublisherAware, ApplicationContextAware {

    private ApplicationEventPublisher publisher ;

    private ApplicationContext applicationContext ;

    public void by(Order order) {
        System.out.println("order success: " + order);

        publisher.publishEvent(new OrderSuccessEvent(order));
        applicationContext.publishEvent(new OrderSuccessEvent(order));
    }

    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.publisher = applicationEventPublisher ;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext ;
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值