亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解spring中的Aware接口功能

 更新時(shí)間:2022年02月15日 09:26:26   作者:jiangfullll  
Spring的依賴注入的最大亮點(diǎn)是所有的Bean對Spring容器的存在是沒有意識的,我們可以將Spring容器換成其他的容器,Spring容器中的Bean的耦合度因此也是極低的,本文重點(diǎn)給大家介紹spring中的Aware接口,感興趣的朋友一起看看吧

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

一,ApplicationContextAware

獲取spring容器,用來訪問容器中定義的其他bean。實(shí)現(xiàn)接口方法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應(yīng)用上下文環(huán)境
    private static ApplicationContext applicationContext;
    /**
     * 實(shí)現(xiàn)ApplicationContextAware接口的回調(diào)方法,設(shè)置上下文環(huán)境
     */
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
     * 獲取對象 這里重寫了bean方法,起主要作用
     *
     * @param name
     * @return  Object 一個(gè)以所給名字注冊的bean的實(shí)例
     * @throws BeansException
    public static Object getBean(String beanId) throws BeansException {
        return applicationContext.getBean(beanId);
}

二、ApplicationEventPublisherAware

這是一個(gè)事件通知發(fā)布接口,實(shí)現(xiàn)public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)方法。實(shí)現(xiàn)ApplicationListener<ApplicationEvent>接口的類在onApplicationEvent(ApplicationEvent event)方法中可以監(jiān)聽到這個(gè)事件通知。

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

定義事件:

package com.zghw.spring.demo.demo.event;
 
import org.springframework.context.ApplicationEvent;
/**
 * 定義一個(gè)發(fā)送短信的事件
 * 實(shí)現(xiàn)了ApplicationEvent
 * @author zghw
 *
 */
public class SendMessageEvent extends ApplicationEvent {
    private static final long serialVersionUID = 1L;
    //消息對象
    private Message message;
     
    //source代表了發(fā)布該事件的發(fā)布源
    public SendMessageEvent(Object source,Message message) {
        super(source);
        this.message = message;
    }
    public Message getMessage() {
        return message;
    public void setMessage(Message message) {
}

  定義監(jiān)聽器觀察者:

package com.zghw.spring.demo.demo.event;
 
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
 * 發(fā)送短信監(jiān)聽器,監(jiān)聽到事件就開始發(fā)送。
 * 實(shí)現(xiàn)ApplicationListener
 * @author zghw
 *
 */
@Component
public class SendMessageListenter implements ApplicationListener<SendMessageEvent>{
    /**
     * 監(jiān)聽事件SendMessage,當(dāng)有事件發(fā)生則調(diào)用該方法
     */
    public void onApplicationEvent(SendMessageEvent event) {
        Message message = event.getMessage();
        String msg=message.getMessage();
        String phone = message.getPhone();
        try {
            System.out.println("開始向手機(jī)"+phone+"發(fā)送短信,短信內(nèi)容為:"+msg);
            Thread.sleep(1000);
            System.out.println("發(fā)送短信成功!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

  定義事件注冊中心以及發(fā)布事件主題:

package com.zghw.spring.demo.demo.event;
 
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
/**
 * 實(shí)現(xiàn)ApplicationEventPublisherAware讓容器ApplicationContext作為事件發(fā)布中心,
 * 因?yàn)锳pplicationContext實(shí)現(xiàn)了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);
        //發(fā)布中心發(fā)布事件
        publisher.publishEvent(event);
    }
    /**
     * 實(shí)現(xiàn)ApplicationEventPublisherAware的方法,spring在使用時(shí)UserServicebean對象時(shí)會自動幫我們注入
     * ApplicationEventPublisher的實(shí)現(xiàn)
     */
    public void setApplicationEventPublisher(
            ApplicationEventPublisher applicationEventPublisher) {
        this.publisher = applicationEventPublisher;
}

到此這篇關(guān)于spring中的Aware接口功能詳解的文章就介紹到這了,更多相關(guān)spring中的Aware接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論