• Spring Bean的生命周期例子


    以下例子源于:W3Cschool,在此作记录

    HelloWorld.java

    package com.how2java.w3cschool.beanlife;
    
    public class HelloWorld {
        private String message;
    
        public void getMessage() {
            System.out.println("Your message:" + message);
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public void init() {
            System.out.println("Bean is going through init");
        }
    
        public void destroy() {
            System.out.println("Bean will destory now");
        }
    
    }

    MainApp.java

    package com.how2java.w3cschool.beanlife;                                                                                                           
                                                                                                                                                       
    import org.springframework.context.support.AbstractApplicationContext;                                                                             
    import org.springframework.context.support.ClassPathXmlApplicationContext;                                                                         
                                                                                                                                                       
    public class MainApp {                                                                                                                             
                                                                                                                                                       
        public static void main(String[] args) {                                                                                                       
            AbstractApplicationContext context = new ClassPathXmlApplicationContext("beanlife.xml");                                                   
            HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); //getBean是用来获取applicationContext.xml文件里bean的,括号内写的是对应的bean的id                 
            obj.getMessage();                                                                                                                          
            context.registerShutdownHook();  //需要注册一个在 AbstractApplicationContext 类中声明的关闭 hook 的 registerShutdownHook() 方法。它将确保正常关闭,并且调用相关的 destroy 方法。                                                                                                     
        }                                                                                                                                              
    }                                                                                                                                                  
                                                                                                                                                       

    beanlife.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"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context     
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
       
       <bean id = "helloWorld"
           class = "com.how2java.w3cschool.beanlife.HelloWorld"
           init-method="init" destroy-method="destroy"> 
           <property name="message" value ="Hello World!"></property>
           </bean>
           <!-- init-method 属性指定一个方法,在实例化 bean 时,立即调用该方法,destroy-method 指定一个方法,只有从容器中移除 bean 之后,才能调用该方法。
           并且,上面指定bean对应的类为 HelloWorld,因此会到该bean(类)下去找对应的方法并调用-->
       
     </beans>

    运行结果如下:

    如果具有太多相同名称的初始化或者销毁方法的 Bean,那么并不需要在每一个 bean 上声明初始化方法销毁方法。框架使用元素中的 default-init-method 和 default-destroy-method 属性提供了灵活地配置这种情况,即,在beans的配置中加上default-init-method 和 default-destroy-method 属性即可,无须在每个bean中都写一次对应的inti和destroy方法。

  • 相关阅读:
    Sql Server 2005开发新特性笔记
    动态调用Web Service(ZT)
    web.config详解(配置文件节点说明)
    SQL分页语句
    在SQL Server 2005数据库中更改数据架构
    [转]在WinForm应用程序中实现自动升级
    DataGrid 导出 EXCEL(简单,实用)
    在WinForm中使用WebServices来实现软件自动升级(AutoUpdate)(C#)
    工作小结TextBox为密码框赋值技巧,CSS溢出显示省略号方法
    自动更新程序源码下载(C#.Net)
  • 原文地址:https://www.cnblogs.com/Guhongying/p/10594290.html
Copyright © 2020-2023  润新知