詳解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í)會(huì)自動(dòng)幫我們注入
* 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)文章
實(shí)例詳解Java實(shí)現(xiàn)圖片與base64字符串之間的轉(zhuǎn)換
這篇文章主要介紹了Java實(shí)現(xiàn)圖片與base64字符串之間的轉(zhuǎn)換實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-12-12
spring配置文件中util:properties和context:property-placeholder用法
這篇文章主要介紹了spring配置文件中util:properties和context:property-placeholder用法,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
SpringSecurity框架下實(shí)現(xiàn)CSRF跨站攻擊防御的方法
CSRF是一種網(wǎng)絡(luò)攻擊方式,也可以說是一種安全漏洞,這種安全漏洞在web開發(fā)中廣泛存在。這篇文章主要介紹了SpringSecurity框架下實(shí)現(xiàn)CSRF跨站攻擊防御,需要的朋友可以參考下2019-12-12
Spring Boot集群管理工具KafkaAdminClient使用方法解析
這篇文章主要介紹了Spring Boot集群管理工具KafkaAdminClient使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
詳解java一維數(shù)組及練習(xí)題實(shí)例
在本篇文章里小編給大家整理了關(guān)于java一維數(shù)組及練習(xí)題的相關(guān)知識點(diǎn)和實(shí)例代碼,有需要的朋友們跟著學(xué)習(xí)下。2019-07-07
關(guān)于ArrayList初始創(chuàng)建設(shè)定長度問題
在使用ArrayList時(shí),初始化長度并不等同于直接設(shè)定數(shù)組大小,如通過構(gòu)造函數(shù)指定長度,僅僅是在內(nèi)部開辟了相應(yīng)的存儲空間,并不會(huì)改變ArrayList的實(shí)際元素個(gè)數(shù),即size屬性仍然為0,因此,嘗試直接訪問未實(shí)際添加元素的位置會(huì)引發(fā)異常2024-11-11
Java HashMap源碼及并發(fā)環(huán)境常見問題解決
這篇文章主要介紹了Java HashMap源碼及并發(fā)環(huán)境常見問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09

