zoukankan      html  css  js  c++  java
  • 学习 Spring (四) Bean 的生命周期

    Spring入门篇 学习笔记

    定义 --> 初始化 --> 使用 --> 销毁

    初始化

    1. 实现 org.springframework.beans.factory.InitializingBean 接口,覆盖 afterPropertiesSet 方法

      public class ExampleBean implements InitializingBean{
      	public void afterPropertiesSet() throws Exception{
      	// do some initialization work
      	}
      	
      }
      
    2. 配置 init-method:

      <bean id="exampleInitBean" class="example.ExampleBean" init-method="start"/>
      
      public class ExampleBean{
      	public void start(){
      	// do some initialization work
      	}
      	
      }
      

    销毁

    1. 实现 org.springframework.beans.factory.DisposableBean 接口,覆盖 destory 方法

      public class ExampleBean implements DisposableBean{
      	public void destory() throws Exception{
      	// do some destruction work
      	}
      	
      }
      
    2. 配置 destory-method

      <bean id="exampleInitBean" class="example.ExampleBean" destory-method="stop"/>
      
      public class ExampleBean{
      	public void stop(){
      	// do some destruction work
      	}
      	
      }
      

    配置全局默认初始化、销毁方法

    <?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" 
    		default-init-method="init" default-destory-method="destory">
    	
     </beans>
    

    多种方式同时使用执行顺序

    1. afterPropertiesSet()
    2. init-method / default-init-method
    3. destory()
    4. destory-method / default-destory-method
    • 如果同时配置了 init-method 和 default-init-method 或 destory-method 和 default-destory-method,default-init-method 或 default-destory-method 不执行
    • default-init-method 和 default-destory-method 可以不在 Bean 中声明

    源码:learning-spring

  • 相关阅读:
    Spring2——特殊值的注入问题、自动装配、使用注解定义bean
    关于ueditor配置单图无法上传的问题
    初始WebApi(3)
    WebApi参数检查验证FluentValidation的使用方法
    js获取兄弟之间的标签
    C#集合ArrayList、泛型集合List(3)
    C#栈、堆的理解(2)
    C#值类型、引用类型(1)
    字符串截取的几种方法
    MVC结合Ajax实现简单的批量删除
  • 原文地址:https://www.cnblogs.com/victorbu/p/10419153.html
Copyright © 2011-2022 走看看