詳解spring中的Aware接口功能
在spring中有很多以XXXAware命名的接口,很多人也不清楚這些接口都是做什么用的,這篇文章將描述常用的一些接口。
一,ApplicationContextAware
獲取spring容器,用來訪問容器中定義的其他bean。實現(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應用上下文環(huán)境 private static ApplicationContext applicationContext; /** * 實現(xiàn)ApplicationContextAware接口的回調方法,設置上下文環(huán)境 */ 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
這是一個事件通知發(fā)布接口,實現(xiàn)public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)方法。實現(xiàn)ApplicationListener<ApplicationEvent>接口的類在onApplicationEvent(ApplicationEvent event)方法中可以監(jiān)聽到這個事件通知。
eg: 源碼來源:http://m.blog.csdn.net/article/details?id=50970667
定義事件:
package com.zghw.spring.demo.demo.event; import org.springframework.context.ApplicationEvent; /** * 定義一個發(fā)送短信的事件 * 實現(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ā)送。 * 實現(xiàn)ApplicationListener * @author zghw * */ @Component public class SendMessageListenter implements ApplicationListener<SendMessageEvent>{ /** * 監(jiān)聽事件SendMessage,當有事件發(fā)生則調用該方法 */ public void onApplicationEvent(SendMessageEvent event) { Message message = event.getMessage(); String msg=message.getMessage(); String phone = message.getPhone(); try { System.out.println("開始向手機"+phone+"發(fā)送短信,短信內容為:"+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; /** * 實現(xiàn)ApplicationEventPublisherAware讓容器ApplicationContext作為事件發(fā)布中心, * 因為ApplicationContext實現(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); } /** * 實現(xiàn)ApplicationEventPublisherAware的方法,spring在使用時UserServicebean對象時會自動幫我們注入 * ApplicationEventPublisher的實現(xiàn) */ public void setApplicationEventPublisher( ApplicationEventPublisher applicationEventPublisher) { this.publisher = applicationEventPublisher; }
到此這篇關于spring中的Aware接口功能詳解的文章就介紹到這了,更多相關spring中的Aware接口內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring學習筆記1之IOC詳解盡量使用注解以及java代碼
這篇文章主要介紹了Spring學習筆記1之IOC詳解盡量使用注解以及java代碼 的相關資料,需要的朋友可以參考下2016-07-07Java報錯:ClassCastException問題解決方法
異常是程序中的一些錯誤,但并不是所有的錯誤都是異常,并且錯誤有時候是可以避免的,下面這篇文章主要給大家介紹了關于Java報錯:ClassCastException問題解決方法,需要的朋友可以參考下2024-07-07Spring Boot+Mybatis+Druid+PageHelper實現(xiàn)多數(shù)據(jù)源并分頁的方法
這篇文章主要給大家介紹了關于Spring Boot+Mybatis+Druid+PageHelper實現(xiàn)多數(shù)據(jù)源并分頁的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們來一起看看吧2018-05-05Java System.currentTimeMillis()時間的單位轉換與計算方式案例詳解
這篇文章主要介紹了Java System.currentTimeMillis()時間的單位轉換與計算方式案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-08-08spring-boot-maven-plugin 插件的作用詳解
添加了spring-boot-maven-plugin插件后,當運行maven打包的命令,項目會被打包成一個可以直接運行的jar包,使用"java -jar"可以直接運行。這篇文章主要給大家介紹spring-boot-maven-plugin 插件的作用,感興趣的朋友一起看看吧2018-10-10springboot獲取properties屬性值的多種方式總結
這篇文章主要介紹了springboot獲取properties屬性值的多種方式總結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03