zoukankan      html  css  js  c++  java
  • 06_init()和destroy()方法

    【工程截图】

    【HelloWorld.java】

    package com.HigginCui;
    
    public class HelloWorld {
        public HelloWorld(){
            System.out.println("Create Object...");
        }
        
        public void init(){
            System.out.println("init method...");
        }
        
        public void hello(){
            System.out.println("hello world...");
        }
        
        public void destroy(){
            System.out.println("destroy method...");
        }
    }

    【applicationContext.xml】

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:aop="http://www.springframework.org/schema/aop"
           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-2.5.xsd
               http://www.springframework.org/schema/aop 
               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
        <!-- 
            init-method:初始化方法
            destroy-method:销毁方法
         -->    
         <bean id="helloWorld"
                class="com.HigginCui.HelloWorld"
                init-method="init"
                destroy-method="destroy">
        </bean>
    </beans>

    【testHelloWorld.java】

    package com.HigginCui.test;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.HigginCui.HelloWorld;
    
    public class testHelloWorld {
        @Test
        public void test(){
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            HelloWorld helloWorld=(HelloWorld) context.getBean("helloWorld");
            helloWorld.hello();
            
            //关闭Spring容器(为了执行destroy方法,一般spring不关闭)
            ClassPathXmlApplicationContext applicationContext=(ClassPathXmlApplicationContext) context;
            applicationContext.close();
        }
    }

    【运行结果】

    2016-6-10 15:23:34 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@26ee7a14: display name [org.springframework.context.support.ClassPathXmlApplicationContext@26ee7a14]; startup date [Fri Jun 10 15:23:34 CST 2016]; root of context hierarchy
    2016-6-10 15:23:34 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [applicationContext.xml]
    2016-6-10 15:23:34 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
    信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@26ee7a14]: org.springframework.beans.factory.support.DefaultListableBeanFactory@2b5575e0
    2016-6-10 15:23:34 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2b5575e0: defining beans [helloWorld]; root of factory hierarchy
    Create Object...
    init method...
    hello world...
    2016-6-10 15:23:34 org.springframework.context.support.AbstractApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@26ee7a14: display name [org.springframework.context.support.ClassPathXmlApplicationContext@26ee7a14]; startup date [Fri Jun 10 15:23:34 CST 2016]; root of context hierarchy
    2016-6-10 15:23:34 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
    信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2b5575e0: defining beans [helloWorld]; root of factory hierarchy
    destroy method...

    【小结】

    从运行结果可以看出,先运行HelloWorld()构造方法,然后运行init()初始化方法,然后再运行调用的hello()的方法,最后关闭spring容器时运行destroy()销毁方法。

  • 相关阅读:
    dns
    sqlserver中int 类型的字段,值为null的时候引发一个问题
    后台读取数据库中值,返回的值出乎意料,并引发了StackOverflowException
    软件设计原则的一些学习总结
    Python基础(5):模块
    SQL 知识纲要
    Linux编程学习笔记Devices
    Python基础(3):函数
    Python基础(2):控制结构
    Python基础(4):类
  • 原文地址:https://www.cnblogs.com/HigginCui/p/5573720.html
Copyright © 2011-2022 走看看