亲宝软件园·资讯

展开

spring Aware接口

jiangfullll 人气:0

在spring中有很多以XXXAware命名的接口,很多人也不清楚这些接口都是做什么用的,这篇文章将描述常用的一些接口。

一,ApplicationContextAware

获取spring容器,用来访问容器中定义的其他bean。实现接口方法public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {}

eg:

package org.company.xxx;
 
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
 * 获取spring容器,以访问容器中定义的其他bean
 */
public class SpringContextUtil implements ApplicationContextAware {
    // Spring应用上下文环境
    private static ApplicationContext applicationContext;
    /**
     * 实现ApplicationContextAware接口的回调方法,设置上下文环境
     */
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
     * 获取对象 这里重写了bean方法,起主要作用
     *
     * @param name
     * @return  Object 一个以所给名字注册的bean的实例
     * @throws BeansException
    public static Object getBean(String beanId) throws BeansException {
        return applicationContext.getBean(beanId);
}

二、ApplicationEventPublisherAware

这是一个事件通知发布接口,实现public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)方法。实现ApplicationListener<ApplicationEvent>接口的类在onApplicationEvent(ApplicationEvent event)方法中可以监听到这个事件通知。

eg: 源码来源:http://m.blog.csdn.net/article/details?id=50970667

定义事件:

package com.zghw.spring.demo.demo.event;
 
import org.springframework.context.ApplicationEvent;
/**
 * 定义一个发送短信的事件
 * 实现了ApplicationEvent
 * @author zghw
 *
 */
public class SendMessageEvent extends ApplicationEvent {
    private static final long serialVersionUID = 1L;
    //消息对象
    private Message message;
     
    //source代表了发布该事件的发布源
    public SendMessageEvent(Object source,Message message) {
        super(source);
        this.message = message;
    }
    public Message getMessage() {
        return message;
    public void setMessage(Message message) {
}

  定义监听器观察者:

package com.zghw.spring.demo.demo.event;
 
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
 * 发送短信监听器,监听到事件就开始发送。
 * 实现ApplicationListener
 * @author zghw
 *
 */
@Component
public class SendMessageListenter implements ApplicationListener<SendMessageEvent>{
    /**
     * 监听事件SendMessage,当有事件发生则调用该方法
     */
    public void onApplicationEvent(SendMessageEvent event) {
        Message message = event.getMessage();
        String msg=message.getMessage();
        String phone = message.getPhone();
        try {
            System.out.println("开始向手机"+phone+"发送短信,短信内容为:"+msg);
            Thread.sleep(1000);
            System.out.println("发送短信成功!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

  定义事件注册中心以及发布事件主题:

package com.zghw.spring.demo.demo.event;
 
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
/**
 * 实现ApplicationEventPublisherAware让容器ApplicationContext作为事件发布中心,
 * 因为ApplicationContext实现了ApplicationEventPublisher
 * @author zghw
 *
 */
@Service
public class UserService implements ApplicationEventPublisherAware{
    private ApplicationEventPublisher publisher;
     
    public void registerUser(String name,String phone) throws InterruptedException{
        System.out.println("注册用户中");
        Thread.sleep(300);
        System.out.println("注册完成!");
         
        Message message=new Message();
        message.setMessage("你好,"+name+" 你中了1000W");
        message.setPhone(phone);
        SendMessageEvent event=new SendMessageEvent(this,message);
        //发布中心发布事件
        publisher.publishEvent(event);
    }
    /**
     * 实现ApplicationEventPublisherAware的方法,spring在使用时UserServicebean对象时会自动帮我们注入
     * ApplicationEventPublisher的实现
     */
    public void setApplicationEventPublisher(
            ApplicationEventPublisher applicationEventPublisher) {
        this.publisher = applicationEventPublisher;
}

加载全部内容

相关教程
猜你喜欢
用户评论