詳解spring中的Aware接口功能
在spring中有很多以XXXAware命名的接口,很多人也不清楚這些接口都是做什么用的,這篇文章將描述常用的一些接口。
一,ApplicationContextAware
獲取spring容器,用來(lái)訪問(wèn)容器中定義的其他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容器,以訪問(wèn)容器中定義的其他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;
* 獲取對(duì)象 這里重寫(xiě)了bean方法,起主要作用
*
* @param name
* @return Object 一個(gè)以所給名字注冊(cè)的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)聽(tīng)到這個(gè)事件通知。
eg: 源碼來(lái)源: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;
//消息對(duì)象
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)聽(tīng)器觀察者:
package com.zghw.spring.demo.demo.event;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* 發(fā)送短信監(jiān)聽(tīng)器,監(jiān)聽(tīng)到事件就開(kāi)始發(fā)送。
* 實(shí)現(xiàn)ApplicationListener
* @author zghw
*
*/
@Component
public class SendMessageListenter implements ApplicationListener<SendMessageEvent>{
/**
* 監(jiān)聽(tīng)事件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("開(kāi)始向手機(jī)"+phone+"發(fā)送短信,短信內(nèi)容為:"+msg);
Thread.sleep(1000);
System.out.println("發(fā)送短信成功!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}定義事件注冊(cè)中心以及發(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("注冊(cè)用戶中");
Thread.sleep(300);
System.out.println("注冊(cè)完成!");
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對(duì)象時(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaWeb響應(yīng)下載功能實(shí)例代碼(包含工具類)
今天通過(guò)本文給大家分享的是關(guān)于javaweb的響應(yīng)(response)下載功能,需要的朋友參考下吧2017-07-07
Spring學(xué)習(xí)筆記1之IOC詳解盡量使用注解以及java代碼
這篇文章主要介紹了Spring學(xué)習(xí)筆記1之IOC詳解盡量使用注解以及java代碼 的相關(guān)資料,需要的朋友可以參考下2016-07-07
Java報(bào)錯(cuò):ClassCastException問(wèn)題解決方法
異常是程序中的一些錯(cuò)誤,但并不是所有的錯(cuò)誤都是異常,并且錯(cuò)誤有時(shí)候是可以避免的,下面這篇文章主要給大家介紹了關(guān)于Java報(bào)錯(cuò):ClassCastException問(wèn)題解決方法,需要的朋友可以參考下2024-07-07
Spring Boot+Mybatis+Druid+PageHelper實(shí)現(xiàn)多數(shù)據(jù)源并分頁(yè)的方法
這篇文章主要給大家介紹了關(guān)于Spring Boot+Mybatis+Druid+PageHelper實(shí)現(xiàn)多數(shù)據(jù)源并分頁(yè)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們來(lái)一起看看吧2018-05-05
Java System.currentTimeMillis()時(shí)間的單位轉(zhuǎn)換與計(jì)算方式案例詳解
這篇文章主要介紹了Java System.currentTimeMillis()時(shí)間的單位轉(zhuǎn)換與計(jì)算方式案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
spring-boot-maven-plugin 插件的作用詳解
添加了spring-boot-maven-plugin插件后,當(dāng)運(yùn)行maven打包的命令,項(xiàng)目會(huì)被打包成一個(gè)可以直接運(yùn)行的jar包,使用"java -jar"可以直接運(yùn)行。這篇文章主要給大家介紹spring-boot-maven-plugin 插件的作用,感興趣的朋友一起看看吧2018-10-10
springboot獲取properties屬性值的多種方式總結(jié)
這篇文章主要介紹了springboot獲取properties屬性值的多種方式總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

