首页 > 其他分享 >Spring接口简单使用

Spring接口简单使用

时间:2023-06-24 17:22:06浏览次数:29  
标签:ApplicationContextAware ApplicationContext applicationContext Spring 接口 简单 Dispo

Spring接口简单使用

ApplicationContextAware

ApplicationContextAware 是一个 Spring 接口,用于在 Spring 应用程序中获取 ApplicationContext 对象。ApplicationContext 是 Spring 框架对 Bean 的管理容器,它负责加载、配置和管理应用程序中的 Bean。

使用 ApplicationContextAware 接口,你可以在任何实现了该接口的类中获取到当前的 ApplicationContext 对象,从而可以访问 Spring 容器中的其他 Bean。

要使用 ApplicationContextAware,请遵循以下步骤:

  1. 创建一个类并实现 ApplicationContextAware 接口。例如:
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class MyApplicationContextAware implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        MyApplicationContextAware.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
}
  1. 在实现类中,创建一个静态的 ApplicationContext 变量,并实现 setApplicationContext() 方法。在该方法中,将传入的 ApplicationContext 赋值给静态变量。
  2. 在实现类中,创建一个静态的 getApplicationContext() 方法,用于获取 ApplicationContext 对象。
  3. 注册实现类为 Spring 组件,可以使用 @Component 或其他适当的注解。

现在,你可以在任何需要获取 ApplicationContext 的地方使用 MyApplicationContextAware.getApplicationContext() 方法来获取实例化的 ApplicationContext 对象。

注意事项:

  • 当应用程序上下文初始化时,Spring 框架将自动检测并调用实现了 ApplicationContextAware 接口的类的 setApplicationContext() 方法,并将 ApplicationContext 对象传递给该方法。
  • 你可以在多个类中实现 ApplicationContextAware 接口来获得相同的 ApplicationContext 对象。
  • 如果你的类需要在初始化之前就能够访问 ApplicationContext,你可以考虑实现 BeanFactoryAware 接口代替 ApplicationContextAware 接口,它提供了访问 BeanFactory 的方法。

通过使用 ApplicationContextAware 接口,你可以在 Spring 应用程序中方便地获取 ApplicationContext 对象,以进一步访问和管理容器中的其他 Bean。

DisposableBean

DisposableBean 是 Spring 的一个接口,用于在 Spring 容器销毁时执行一些清理任务。当 Spring 容器关闭时,会调用实现了 DisposableBean 接口的 Bean 的 destroy() 方法。

使用 DisposableBean 接口,你可以在 Bean 销毁前进行一些额外的清理工作,例如关闭打开的数据库连接、释放资源等。

要使用 DisposableBean,请遵循以下步骤:

  1. 创建一个类并实现 DisposableBean 接口。例如:

java

复制

import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;

@Component
public class MyDisposableBean implements DisposableBean {

    @Override
    public void destroy() throws Exception {
        // 执行清理任务
        System.out.println("Cleaning up resources...");
    }
}
  1. 在实现类中,实现 destroy() 方法。在该方法中,编写你需要在 Bean 销毁前执行的清理任务。
  2. 注册实现类为 Spring 组件,可以使用 @Component 或其他适当的注解。

现在,当 Spring 容器关闭时,会自动调用 MyDisposableBeandestroy() 方法,并执行你编写的清理任务。

注意事项:

  • 实现 DisposableBean 接口的 Bean 可能会与其他生命周期回调方法(例如 @PreDestroy 注解或自定义的 init() 方法)发生冲突。如果你的 Bean 已经使用了其他方式来定义销毁方法,那么实现 DisposableBean 可能不是必需的。
  • 如果你使用 XML 配置文件来定义 Bean,你可以使用 <destroy-method> 元素来指定销毁方法,而无需显式实现 DisposableBean 接口。
  • 与实现 DisposableBean 接口相比,使用 @PreDestroy 注解来标记销毁方法更加推荐。@PreDestroy 注解提供了更好的可读性和灵活性,并且不依赖于 Spring 框架特定的接口。

通过使用 DisposableBean 接口,你可以在 Spring 容器销毁时执行一些清理任务,确保应用程序资源的正确释放和关闭。

我们可以使用ApplicationContextAware和DisposableBean接口来创建一个工具类从而获取或销毁容器中的Bean对象。

@Slf4j
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {

    private static ApplicationContext applicationContext = null;

    /**
     * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
     */
    public static <T> T getBean(Class<T> requiredType) {
        assertContextInjected();
        return applicationContext.getBean(requiredType);
    }

    /**
     * 检查ApplicationContext不为空.
     */
    private static void assertContextInjected() {
        if (applicationContext == null) {
            throw new IllegalStateException("applicaitonContext属性未注入, 请在applicationContext" +
                    ".xml中定义SpringContextHolder或在SpringBoot启动类中注册SpringContextHolder.");
        }
    }

    /**
     * 清除SpringContextHolder中的ApplicationContext为Null.
     */
    private static void clearHolder() {
        log.debug("清除SpringContextHolder中的ApplicationContext:"
                + applicationContext);
        applicationContext = null;
    }

    @Override
    public void destroy(){
        SpringContextHolder.clearHolder();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringContextHolder.applicationContext != null) {
            log.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:" + SpringContextHolder.applicationContext);
        }
        SpringContextHolder.applicationContext = applicationContext;
    }
}

标签:ApplicationContextAware,ApplicationContext,applicationContext,Spring,接口,简单,Dispo
From: https://www.cnblogs.com/zzprogram/p/17501359.html

相关文章

  • 微服务 – Spring Cloud – Gateway
    微服务–SpringCloud–GatewayApi网关(ApiGateway)微服务可能分布在不同的主机上,这样有许多缺点:前端需要硬编码调用不同地址的微服务很麻烦;存在跨域访问的问题;微服务地址直接暴露是不安全的。还有所以需要为前端提供一个统一的访问入口。Gateway就是用于解决以上问题的框......
  • Apollo2.1.0+Springboot使用OpenApI
    依赖管理<!--bootstrap最高级启动配置读取--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId><version>3.1.3</v......
  • springcloud项目启动后立即停止
    刚学完springcloud,只看了一遍课没有动手实践,现在第一次自己动手操作的时候不知道该选哪些依赖该怎么配置。依赖全选上后因为各自配置问题运行不起来,只能先从最简单的开始慢慢加依赖。这次只选了nacos和feign,成功运行,项目也没有报错,但是运行后项目立马停止。 百度了一下原因......
  • springboot使用Websocket写一个聊天室
     1<!--websocket依赖-->2<dependency>3<groupId>org.springframework.boot</groupId>4<artifactId>spring-boot-starter-websocket</artifactId>5</dependency>目录 ......
  • 怎么选择API接口来获取自己想要的数据
    在今天的数字时代,数据变得越来越重要,API接口也成为了获取数据的一种重要方式。无论是开发自己的应用程序还是进行市场营销,数据的获取都是非常必要的。但是,如何选择API接口来获取自己想要的数据呢?以下是一些有用的建议:1.确定需要的数据类型 在选择API接口之前,首先需确定需要获......
  • RPC接口与Https接口
    RPC(remoteprocedurecall)远程过程调用;Http:超文本传输协议。RPC是一种方法,http是一种协议。RPC服务主体要工作在tcp协议上,而http服务工作在http协议上,由于http协议基于tcp协议,所以rpc服务比http更轻量,效率更高RPC远程过程调用RPC效率优势明显,在实际开发中,客户端和服务端在技术......
  • 基于springboot+vue的漫画之家管理系统,附源码+数据库+论文+PPT,适合课程设计、毕业设计
    1、项目介绍随着信息技术和网络技术的飞速发展,人类已进入全新信息化时代,传统管理技术已无法高效,便捷地管理信息。为了迎合时代需求,优化管理效率,各种各样的管理系统应运而生,各行各业相继进入信息管理时代,“漫画之家”系统就是信息时代变革中的产物之一。任何系统都要遵循系统设计......
  • 创建自定义的Spring Boot Starter
    1.概述Springboot的开发人员给那些流行的开源项目提供了很多Starter,但是我们并不局限于这些。我们可以创建自己的Starter,如果我们有一个公司内部使用的代码库,如果我们实在Springboot项目中使用,那给这个代码库创建一个SpringbootStarter是一个很好的实践。这些自定义的Sta......
  • spring 学习框架知识结构
    b站上,spring开发实战,书籍配套视频https://www.bilibili.com/video/BV1144y1g7Q2/?spm_id_from=333.999.0.0&vd_source=debae4e77e1cafd283cb9668d2acb3a7......
  • springboot 学习框架
    李兴华,springboot开发书籍配套视频https://www.bilibili.com/video/BV1wL411H7W8?p=172&vd_source=debae4e77e1cafd283cb9668d2acb3a7......