zoukankan      html  css  js  c++  java
  • 初生牛犊之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("销毁成功");       
        }
    }

     

  • 相关阅读:
    @ModelAttribute 与@InitBinder
    springboot开启矩阵传参MatrixVariable
    socket(一)
    request请求《一》
    Ajax请求中的async:false/true的作用
    java.lang.NullPointerException at org.apache.jsp.index_jsp._jspInit(index_jsp.java:40)
    shiro登录源码
    js(正则验证)
    多进程之间的通信
    队列中常用方法的使用
  • 原文地址:https://www.cnblogs.com/yaoxing92/p/2995818.html
Copyright © 2011-2022 走看看