• 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.
  • 相关阅读:
    【转】请收下这72个炫酷的CSS技巧
    【转】css套路学习(一)
    【转】用最短的CSS样式,勾勒大数据演示屏
    【转】CSS 实现按钮点击动效的套路
    Docker Redis分布式存储
    Dockerfile的使用以及docker部署java项目
    centos7安装Docker详细步骤(无坑版教程)
    mysql三种备份方式
    CentOS7升级OpenSSL版本 前置机Linux安装Nginx
    go的nil值/零值/空值
  • 原文地址:https://www.cnblogs.com/zylq-blog/p/7792998.html
Copyright © 2020-2023  润新知