Spring之InitializingBean接口和DisposableBean接口介绍

简介: 本文来介绍下InitializingBean接口和DisposableBean接口的作用


 本文来介绍下InitializingBean接口和DisposableBean接口的作用

文章目录

   1.InitializingBean

   2.DisposableBean

   3.案例演示

1.InitializingBean

 该接口的作用是:允许一个bean在它的所有必须属性被BeanFactory设置后,来执行初始化的工作,该接口中只有一个方法,afterPropertiesSet

public interface InitializingBean {
  /**
   * Invoked by the containing {@code BeanFactory} after it has set all bean properties
   * and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
   * <p>This method allows the bean instance to perform validation of its overall
   * configuration and final initialization when all bean properties have been set.
   * @throws Exception in the event of misconfiguration (such as failure to set an
   * essential property) or if initialization fails for any other reason
   */
  void afterPropertiesSet() throws Exception;
}

2.DisposableBean

 该接口的作用是:允许在容器销毁该bean的时候获得一次回调。DisposableBean接口也只规定了一个方法:destroy

public interface DisposableBean {
  /**
   * Invoked by the containing {@code BeanFactory} on destruction of a bean.
   * @throws Exception in case of shutdown errors. Exceptions will get logged
   * but not rethrown to allow other beans to release their resources as well.
   */
  void destroy() throws Exception;
}

3.案例演示

/**
 * 实现InitializingBean和DisposableBean接口
 * @author dengp
 *
 */
public class User implements InitializingBean,DisposableBean{
  private int id;
  private String name;
  private String beanName;
  public User(){
    System.out.println("User 被实例化");
  }
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    System.out.println("设置:"+name);
    this.name = name;
  }
  public String getBeanName() {
    return beanName;
  }
  public void setBeanName(String beanName) {
    this.beanName = beanName;
  }
  @Override
  public String toString() {
    return "User [id=" + id + ", name=" + name + ", beanName=" + beanName + "]";
  }
  @Override
  public void destroy() throws Exception {
    // TODO Auto-generated method stub
    System.out.println("destory ....");
  }
  @Override
  public void afterPropertiesSet() throws Exception {
    System.out.println("afterPropertiesSet....");
  }
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean class="com.dpb.pojo.User" id="user" >
    <property name="name" value="波波烤鸭"></property>
  </bean>
</beans>

测试代码

@Test
public void test1() {
  ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
  User user = ac.getBean(User.class);
  System.out.println(user);
  ac.registerShutdownHook();
}

输出结果:

User 被实例化
设置:波波烤鸭
afterPropertiesSet....
User [id=0, name=波波烤鸭, beanName=null]
destory ....

 通过输出能够显示spring初始化bean的时候,如果bean实现了InitializingBean接口,会自动调用afterPropertiesSet方法,在bean被销毁的时候如果实现了DisposableBean接口会自动回调destroy方法后然后再销毁


相关文章
|
XML Java 数据格式
探索Spring之利剑:ApplicationContext接口
本文深入介绍了Spring框架中的核心接口ApplicationContext,解释了其作为应用容器的功能,包括事件发布、国际化支持等,并通过基于XML和注解的配置示例展示了如何使用ApplicationContext管理Bean实例。
|
自然语言处理 JavaScript Java
Spring 实现 3 种异步流式接口,干掉接口超时烦恼
本文介绍了处理耗时接口的几种异步流式技术,包括 `ResponseBodyEmitter`、`SseEmitter` 和 `StreamingResponseBody`。这些工具可在执行耗时操作时不断向客户端响应处理结果,提升用户体验和系统性能。`ResponseBodyEmitter` 适用于动态生成内容场景,如文件上传进度;`SseEmitter` 用于实时消息推送,如状态更新;`StreamingResponseBody` 则适合大数据量传输,避免内存溢出。文中提供了具体示例和 GitHub 地址,帮助读者更好地理解和应用这些技术。
3200 122
|
存储 NoSQL Java
Spring Boot项目中使用Redis实现接口幂等性的方案
通过上述方法,可以有效地在Spring Boot项目中利用Redis实现接口幂等性,既保证了接口操作的安全性,又提高了系统的可靠性。
999 1
|
存储 数据采集 Java
Spring Boot 3 实现GZIP压缩优化:显著减少接口流量消耗!
在Web开发过程中,随着应用规模的扩大和用户量的增长,接口流量的消耗成为了一个不容忽视的问题。为了提升应用的性能和用户体验,减少带宽占用,数据压缩成为了一个重要的优化手段。在Spring Boot 3中,通过集成GZIP压缩技术,我们可以显著减少接口流量的消耗,从而优化应用的性能。本文将详细介绍如何在Spring Boot 3中实现GZIP压缩优化。
2455 6
|
SQL Java 数据库
实时计算 Flink版产品使用问题之Spring Boot集成Flink可以通过什么方式实现通过接口启动和关闭Flink程序
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
Java 开发者 Spring
Spring项目中Ordered接口的应用:全局过滤器(GlobalFilter)的顺序控制
Spring项目中Ordered接口的应用:全局过滤器(GlobalFilter)的顺序控制
1221 2
|
JavaScript Java Spring
Spring Boot 接口返回文件流
Spring Boot 接口返回文件流
1384 0