广告
返回顶部
首页 > 资讯 > 后端开发 > Python >详解spring中的Aware接口功能
  • 918
分享到

详解spring中的Aware接口功能

2024-04-02 19:04:59 918人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录一,ApplicationContextAware二、ApplicationEventPublisherAware在spring中有很多以XXXAware命名的接口,很多人也不清

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接口内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 详解spring中的Aware接口功能

本文链接: https://www.lsjlt.com/news/139178.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 详解spring中的Aware接口功能
    目录一,ApplicationContextAware二、ApplicationEventPublisherAware在spring中有很多以XXXAware命名的接口,很多人也不清...
    99+
    2022-11-13
  • Spring中的aware接口详情
    Spring中有很多继承于aware中的接口,这些接口到底是做什么用到的。 aware,翻译过来是知道的,已感知的,意识到的,所以这些接口从字面意思应该是能感知到所有Aware前面...
    99+
    2022-11-13
  • Spring Aware接口示例代码详解
    若 Spring 检测到 bean 实现了 Aware 接口,则会为其注入相应的依赖。所以通过让bean 实现 Aware 接口,则能在 bean 中获得相应的 Spring 容器资...
    99+
    2022-11-13
  • 详解Spring中InitializingBean接口的功能
    Spring的InitializingBean接口有很好的用处,位于spring beans中,它只提供一个方法afterPropertiesSet(),当你实现了该方法后,spri...
    99+
    2022-11-13
  • Spring的Aware接口实现及执行顺序详解
    目录一、实现了Aware的接口二、为什么要使用 Aware 接口三、Aware接口执行顺序一、实现了Aware的接口 Spring中有很多继承于aware中的接口,这些接口到底是做什...
    99+
    2022-12-26
    Spring Aware接口执行顺序 Spring Aware
  • Spring中Aware接口的实现原理是什么
    今天就跟大家聊聊有关Spring中Aware接口的实现原理是什么,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。使用示例:@Component&nbs...
    99+
    2022-10-19
  • Spring中的bean怎么利用Aware接口获取
    这期内容当中小编将会给大家带来有关Spring中的bean怎么利用Aware接口获取,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。在使用spring编程时,常常会遇到想根据bean的名称来获取相应的bea...
    99+
    2023-05-31
    spring bean aware
  • Spring Boot 接口加解密功能实现
    目录介绍基础知识hutool-crypto加密解密工具request流只能读取一次的问题问题描述解决办法SpringBoot的参数校验validation自定义starterRequ...
    99+
    2023-05-18
    Spring Boot 接口加解密 Spring Boot 接口 Spring Boot 加解密
  • Spring详细讲解FactoryBean接口的使用
    目录一、基本使用二、高级使用FactoryBean是一个接口,创建对象的过程使用了工厂模式。 一、基本使用 让Spring容器通过FactoryBean来实现对象的创建。 创建Fa...
    99+
    2022-11-13
  • 详解Spring中实现接口动态的解决方法
    前言本文主要给大家介绍的是关于Spring实现接口动态的相关内容,分享出来供大家参考学习,下面话不多说,来一起看看详细的介绍吧。关于这个问题是因为领导最近跟我提了一个需求,是有关于实现类Mybatis的@Select、@Insert注解的功...
    99+
    2023-05-31
    spring 动态接口
  • Spring中@Scheduled功能的使用方法详解
    目录前言一、Spring @Scheduled Annotation1.2 如何启用@Scheduled 注释1.3 使用@Scheduled 注释二、固定的延时和频率使用@Sche...
    99+
    2022-11-13
  • Spring Security实现接口放通的方法详解
    目录1.SpringBoot版本2.实现思路3.实现过程3.1新建注解3.2新建请求枚举类3.3判断Controller方法上是否存在该注解3.4在SecurityConfig上进行...
    99+
    2022-11-13
  • Spring Boot Admin的使用详解(Actuator监控接口)
    目录第一部分 Spring Boot Admin 简介admin-server 服务端(admin-server)客户端第二部分 快速入门服务端配置(admin-server)客户端...
    99+
    2022-11-12
  • JavaWeb实现文件上传和下载接口功能详解
    目录1.上传java代码实现2.文件流下载java代码实现3.Fileutil工具类代码1.上传java代码实现 @ResponseBody @PostMapping...
    99+
    2022-12-27
    Java Web文件上传下载 Java Web文件上传 Java Web文件下载 Java 文件上传下载
  • Java中接口的多态详解
    目录多态参数多态数组接口的多态传递现象总结多态参数 就像我们现实生活中电脑的usb接口,我们既可以接受手机对象,又可以接受相机对象,等等,体现了接口的多态,查看以下代码 接口: pa...
    99+
    2022-11-13
  • Java中接口的深入详解
    目录一、前言二、接口接口的格式三、接口的特点接口的使用四、类与接口的关系接口多重继承的好处练习总结一、前言 前面我们说了抽象类的概述,我们对抽象类也有个认识和理解了,现在我们学习十分...
    99+
    2022-11-12
  • Go语言利用接口实现链表插入功能详解
    目录1. 接口定义1.1 空接口1.2 实现单一接口1.3 接口多方法实现2. 多态2.1 为不同数据类型的实体提供统一的接口2.2 多接口的实现3. 系统接口调用4. 接口嵌套5....
    99+
    2022-11-13
  • Spring Data JPA框架的核心概念与Repository接口详解
    目录1 核心概念CrudRepository接口PagingAndSortingRepository接口2 查询方法3 后续内容介绍1 核心概念 Spring Data存储库抽象的中...
    99+
    2022-11-13
  • 详解Java中Collector接口的组成
    一、Collector常常出现的地方 java8引入了stream,Collector是与stream一起出现的,配合stream使用的好帮手,如果用过stream,我们应该都有写过...
    99+
    2022-11-12
  • golang中的空接口使用详解
    目录1、空接口2、类型断言3、结构体值接收者和指针接收者实现接口的区别4、一个结构体实现多个接口5、接口嵌套6、Golang中空接口和类型断言使用细节1、空接口 Golang 中的接...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作