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方法后然后再销毁


相关文章
|
1月前
|
Java API 微服务
【Spring Boot系列】通过OpenAPI规范构建微服务服务接口
【4月更文挑战第5天】通过OpenAPI接口构建Spring Boot服务RestAPI接口
117 0
|
1月前
|
前端开发 安全 Java
Spring Boot 三招组合拳,手把手教你打出优雅的后端接口
Spring Boot 三招组合拳,手把手教你打出优雅的后端接口
62 0
|
7天前
|
存储 Java 数据安全/隐私保护
Spring Boot中实现邮箱登录/注册接口
Spring Boot中实现邮箱登录/注册接口
|
5天前
|
前端开发 Java 开发者
在Spring框架中,`PathMatcher`是用于进行URL路径匹配的接口
在Spring框架中,`PathMatcher`是用于进行URL路径匹配的接口
25 6
|
5天前
|
算法 Java API
在Spring Boot中实现接口签名验证通常涉及以下步骤
在Spring Boot中实现接口签名验证通常涉及以下步骤
29 4
|
1月前
|
Java Spring
spring boot访问接口报500
spring boot访问接口报500
22 2
|
1月前
|
Java 数据库连接 数据库
spring+mybatis_编写一个简单的增删改查接口
spring+mybatis_编写一个简单的增删改查接口
26 2
|
1月前
|
算法 NoSQL Java
限流艺术:Spring Boot接口限流的实用指南
限流艺术:Spring Boot接口限流的实用指南
301 0
限流艺术:Spring Boot接口限流的实用指南
|
1月前
|
数据采集 前端开发 NoSQL
Spring Boot反爬虫,防止接口盗刷
Spring Boot反爬虫,防止接口盗刷
33 1
|
1月前
ssm(Spring+Spring mvc+mybatis)Dao接口——IDeptDao
ssm(Spring+Spring mvc+mybatis)Dao接口——IDeptDao