zoukankan      html  css  js  c++  java
  • Java框架spring 学习笔记(三):Bean 的生命周期

    当一个 bean 被实例化时,它可能需要执行一些初始化使它转换成可用状态。当bean不再需要,并且从容器中移除时,需要做一些清除工作。为了定义安装和拆卸一个 bean,我们只要声明init-method 和/或 destroy-method 参数。init-method 属性指定一个方法,在实例化 bean 时,立即调用该方法。同样,destroy-method 指定一个方法,只有从容器中移除 bean 之后,才能调用该方法。

    编写HelloWorld.java

     1 package com.example.spring;
     2 
     3 public class HelloWorld {
     4     private String message;
     5     public void setMessage(String message){
     6         this.message  = message;
     7     }
     8 
     9     public void getMessage(){
    10         System.out.println("Your Message : " + message);
    11     }
    12     //定义初始化方法
    13     public void init(){
    14         System.out.println("Bean is going through init.");
    15     }
    16     //定义销毁方法
    17     public void destroy(){
    18         System.out.println("Bean will destroy now.");
    19     }
    20 }

    编写Beans.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     5 
     6     <bean id="helloWorld"
     7           class="com.example.spring.HelloWorld"
     8           init-method="init" destroy-method="destroy">
     9         <property name="message" value="Hello World"></property>
    10     </bean>
    11 </beans>

    编写Application.java

     1 package com.example.spring;
     2 
     3 import org.springframework.context.support.AbstractApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 public class Application {
     7     public static void main(String[] args) {
     8         //bean配置文件所在位置 D:\IdeaProjects\spring\src\Beans.xml
     9         //使用AbstractApplicationContext容器
    10         AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\IdeaProjects\spring\src\Beans.xml");
    11         HelloWorld obj = (HelloWorld)context.getBean("helloWorld");
    12         obj.getMessage();
    13         //registerShutdownHook关闭hook,确保正常关闭,并调用destroy方法
    14         context.registerShutdownHook();
    15     }
    16 }

    运行输出

    Bean is going through init.
    Your Message : Hello World!
    Bean will destroy now.

    默认的初始化和销毁方法

    如果你有太多具有相同名称的初始化或者销毁方法的 Bean,那么你不需要在每一个 bean 上声明初始化方法和销毁方法。框架使用 元素中的 default-init-method 和 default-destroy-method 属性提供了灵活地配置这种情况,如下所示:

    <?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-destroy-method="destroy">
    
        <bean id="helloWorld"
              class="com.example.spring.HelloWorld">
            <property name="message" value="Hello World"></property>
        </bean>
    </beans>

    运行输出

    Bean is going through init.
    Your Message : Hello World!
    Bean will destroy now.
  • 相关阅读:
    事务传播机制,搞懂。
    洛谷 P1553 数字反转(升级版) 题解
    洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is Here 题解
    洛谷 P1055 ISBN号码 题解
    洛谷 P2141 珠心算测验 题解
    洛谷 P1047 校门外的树 题解
    洛谷 P1980 计数问题 题解
    洛谷 P1008 三连击 题解
    HDU 1013 题解
    HDU 1012 题解
  • 原文地址:https://www.cnblogs.com/zylq-blog/p/7792998.html
Copyright © 2011-2022 走看看