广告
返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >Spring init-method与destroy-method属性的用法解析
  • 607
分享到

Spring init-method与destroy-method属性的用法解析

Springinit-method属性destroy-method属性 2021-02-22 00:02:22 607人浏览 绘本
摘要

目录spring init-method与destroy-method属性使用知识点介绍:操作步骤:init-method="init"和 destroy-method="close" 作用Spring init-method与destro

目录
  • spring init-method与destroy-method属性使用
    • 知识点介绍:
    • 操作步骤:
  • init-method="init"和 destroy-method="close" 作用

    Spring init-method与destroy-method属性使用

    知识点介绍:

    有时候在bean初始化之后要执行的初始化方法,以及在bean销毁时执行的方法。这时就需要配置init-method和destroy-method属性,顾名思义,配置初始与销毁的方法。

    操作步骤:

    创建Speaker对象

    
    public class Speaker { 
    	private String name;
    	private String topic;	
    	private Speaker(String name,String topic){
    		this.name = name;
    		this.topic = topic;
    	}
    	
    	
    	private void init() {
    		System.out.println("执行Speaker 的 初始化方法 init");
    	}
     
    	
    	private void destroy() {
    		System.out.println("执行Speaker 的销毁方法 destroy");
    	}
    	
    	public void teach() {		
    		System.out.println(toString());
    	}
    	
    	@Override
    	public String toString() {
    		return "Speaker [name=" + name + ", topic=" + topic + "]";
    	}
    }

    创建Spring配置文件beanLearn05.xml

    
    <?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">
     
     <!-- Learn 05 init-method和destroy-method属性的使用 -->
     <bean id="speaker05" class="com.mahaochen.spring.learn05.Speaker"
      init-method="init" destroy-method="destroy">
      <constructor-arg index="0" value="elle" />
      <constructor-arg index="1" value="Study Hard!" />
     </bean>
    </beans>

    将Spring配置文件beanLearn05.xml引入到主配置文件beans.xml中。

    
    <!-- Learn 04  使用实例工厂方式实例化Bean -->
     <import resource="com/mahaochen/spring/learn05/beanLearn05.xml"/>

    编写测试类TestSpring05.java。

    
    public class TestSpring05 { 
     public static void main(String[] args) {  
      ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
      Speaker speaker05 = (Speaker) appContext.getBean("speaker05");
      speaker05.teach();
      ((ClassPathXmlApplicationContext) appContext).close();
     }
    }

    init-method="init"和 destroy-method="close" 作用

    一般在我们配置数据源的时候,会这样写

    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    

    init-method="init" destroy-method="close" 作用:

    init-method="init"是指bean被初始化时执行的方法,当bean实例化后,执行init-method用于初始化数据库连接池。

    destroy-method="close" 是指bean被销毁时执行的方法 Spring容器关闭时调用该方法即调用close()将连接关闭。

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程界。

    --结束END--

    本文标题: Spring init-method与destroy-method属性的用法解析

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

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

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

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

    下载Word文档
    猜你喜欢
    软考高级职称资格查询
    编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
    • 官方手机版

    • 微信公众号

    • 商务合作