• 初生牛犊之spring(二)


    Bean的生命周期(Bean的生命周期包括定义、初始化、使用和销毁

       1.Bean的定义

           Spring中,通常是通过配置文件的方式来定义Bean的

        2.Bean的初始化

           Bean的初始化可以通过在配置文档中指定属性来完成,也可以实现特定接口来完成。

           1)在配置文件中通过指定init-method属性来完成

    如:<bean id="BeanLife" class="cn.Bean.Life.BeanLife" init-method="init"> <!-- 定义init方法为初始化方法-->

           2)实现org.springframework.beans.factory.InitializingBean接口

    如:

    package cn.Bean.Life;
    import java.util.Date;
    import org.springframework.beans.factory.InitializingBean;
    public class BeanInit implements InitializingBean{
        private String name;
        public String getName() {
            return name;
        }
        public Date getDate() {
            return date;
        }
        private Date date;
        public void afterPropertiesSet() throws Exception {
            System.out.println("接口实现初始化成功");
            this.name="接口实现初始化";
            this.date=new Date();        
        }
    }      

          3.Bean的使用(略)

          4.Bean的销毁

              Bean的初始化也可以通过在配置文档中指定属性来完成,也可以实现特定接口来完成。

              1)在配置文件中通过指定destroy-method属性来完成

    如:<bean id="BeanLife" class="cn.Bean.Life.BeanLife" destroy-method="destroy"> <!--定义destroy方法为销毁方法-->

              2)实现org.springframework.beans.factory.DisposableBean接口

    package cn.Bean.Life;
    import java.util.Date;
    import org.springframework.beans.factory.DisposableBean;
    public class BeanInit implements DisposableBean{
        private String name;
        public String getName() {
            return name;
        }
        public Date getDate() {
            return date;
        }
        private Date date;
        public void destroy() throws Exception {
            System.out.println("销毁成功");       
        }
    }

     

  • 相关阅读:
    u盘安装ubuntu10.04 、11.04 server 分类: arm-linux-Ubuntu 2013-08-12 15:47 504人阅读 评论(0) 收藏
    linux下的framebuffer显示图片 分类: arm-linux-Ubuntu 2013-08-12 15:43 506人阅读 评论(0) 收藏
    Sql Server专题:SQL 经典实例
    1054 求平均值 (20 分)
    1052 卖个萌 (20 分)
    1053 住房空置率 (20 分)
    1050 螺旋矩阵 (25 分
    1051 复数乘法 (15 分)
    1049 数列的片段和 (20 分)
    1046 划拳 (15 分)
  • 原文地址:https://www.cnblogs.com/yaoxing92/p/2995818.html
Copyright © 2020-2023  润新知