在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;
public class SpringContextUtil implements ApplicationContextAware {
// Spring应用上下文环境
private static ApplicationContext applicationContext;
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;
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;
@Component
public class SendMessageListenter implements ApplicationListener<SendMessageEvent>{
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;
@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);
}
public void setApplicationEventPublisher(
ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
到此这篇关于spring中的Aware接口功能详解的文章就介绍到这了,更多相关spring中的Aware接口内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!