zoukankan      html  css  js  c++  java
  • IOC容器-Bean标签的生命周期方法

    什么是生命周期呢?

    一个件事物从生到死的过程,称之为生命周期。

    使用bean标签属性

    init-method="方法名" 
    destroy-method="方法名"

    案例

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Addr {
        private String addr;
    
        public void init() {
            System.out.println("初始化生命周期");
        }
    
        public void destroy() {
            System.out.println("销毁周期");
        }
    }
    <!-- 生命周期 -->
    <bean init-method="init" destroy-method="destroy" id="addrs" class="com.spring.domain.Addr">
       <property name="addr" value="中国北京"></property>
    </bean>
    //生命周期测试
    @Test
    public void test4() {
       ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
       Addr addrs = (Addr) ac.getBean("addrs");
       System.out.println(addrs);
       ((ClassPathXmlApplicationContext) ac).close();
    }

  • 相关阅读:
    模块-和包
    re模块
    递归函数
    内置函数
    C++ 创建文件的方法
    C++多态的实现条件
    C++常见错误总结
    Http客户端跳转和服务器端跳转的区别
    struts1学习
    Java 核心读书笔记 第11章
  • 原文地址:https://www.cnblogs.com/xiaozhang666/p/13619469.html
Copyright © 2011-2022 走看看