首頁 > 軟體

詳解spring中的Aware介面功能

2022-02-15 10:02:07

在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;
}

到此這篇關於spring中的Aware介面功能詳解的文章就介紹到這了,更多相關spring中的Aware介面內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com