详解spring中的Aware接口功能

作者:jiangfullll 时间:2023-07-02 00:36:01 

在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;
/**
* 获取spring容器,以访问容器中定义的其他bean
*/
public class SpringContextUtil implements ApplicationContextAware {
   // Spring应用上下文环境
   private static ApplicationContext applicationContext;
   /**
    * 实现ApplicationContextAware接口的回调方法,设置上下文环境
    */
   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;
/**
* 定义一个发送短信的事件
* 实现了ApplicationEvent
* @author zghw
*
*/
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;
/**
* 发送短信 * ,监听到事件就开始发送。
* 实现ApplicationListener
* @author zghw
*
*/
@Component
public class SendMessageListenter implements ApplicationListener<SendMessageEvent>{
   /**
    * 监听事件SendMessage,当有事件发生则调用该方法
    */
   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;
/**
* 实现ApplicationEventPublisherAware让容器ApplicationContext作为事件发布中心,
* 因为ApplicationContext实现了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);
       //发布中心发布事件
       publisher.publishEvent(event);
   }
   /**
    * 实现ApplicationEventPublisherAware的方法,spring在使用时UserServicebean对象时会自动帮我们注入
    * ApplicationEventPublisher的实现
    */
   public void setApplicationEventPublisher(
           ApplicationEventPublisher applicationEventPublisher) {
       this.publisher = applicationEventPublisher;
}

来源:https://www.cnblogs.com/jiangfullll/articles/5722879.html

标签:spring,Aware,接口
0
投稿

猜你喜欢

  • C语言/C++中如何产生随机数

    2023-06-25 08:48:57
  • Android利用Flutter实现立体旋转效果

    2023-06-20 08:20:32
  • Flutter倒计时/计时器的实现代码

    2023-07-01 03:50:50
  • Flutter Widgets粘合剂CustomScrollView NestedScrollView滚动控件

    2023-07-06 01:24:29
  • 使用flutter创建可移动的stack小部件功能

    2023-06-21 12:28:25
  • jsp如何获取Session中的值

    2023-07-01 05:59:28
  • 10种简单的Java性能优化

    2023-06-20 20:43:41
  • C语言字符串另类用法的实现

    2023-06-19 02:05:25
  • IOS与网页JS交互详解及实例

    2023-07-08 11:58:20
  • 六款值得推荐的android(安卓)开源框架简介

    2023-06-24 01:46:54
  • java.net.SocketException: Connection reset 解决方法

    2023-06-17 01:28:43
  • 完美解决Android Studio集成crashlytics后无法编译的问题

    2023-06-23 16:49:07
  • Flutter 队列任务的实现

    2023-07-07 17:25:14
  • C#创建临时文件的方法

    2023-06-16 14:32:36
  • Android Studio kotlin生成编辑类注释代码

    2023-06-16 12:03:20
  • C# GDI+实现时钟表盘

    2023-06-20 07:11:32
  • Spring Boot深入排查 java.lang.ArrayStoreException异常

    2023-07-11 16:31:27
  • android TextView中识别多个url并分别点击跳转方法详解

    2023-06-21 04:42:32
  • Flutter实现矩形取色器的封装

    2023-06-19 04:08:47
  • Spring MVC 关于controller的字符编码问题

    2023-06-17 09:52:52
  • asp之家 软件编程 m.aspxhome.com