• Spring框架(六)—— spring的Bean的生命周期


    Bean的生命周期:

       定义    就是在xml文件中定义<bean>子标签,定义某个类的对象需要使用bean进行自动管理注入

       初始化   就是使用获取一个Bean容器context,然后使用context调用start方法进行初始化

       使用    就是获取对应bean中的实例

       销毁    就是bean不需要在使用的时候对bean相关的资源信息进行销毁

    初始化+销毁

      方法一


      

      案例(使用了JUnit4进行测试)

     

    package org.bean.example;
    
    public class BeanLifeCycle {
        
        public void start(){
            System.out.println("bean start");
        }
        public void stop(){
            System.out.println("bean stop");
        }
    }
    package org.bean.example;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.BlockJUnit4ClassRunner;
    import org.util.test.UnitTestBase;
    
    @RunWith(BlockJUnit4ClassRunner.class)
    public class TestBeanLifeCycle extends UnitTestBase{
        
        public TestBeanLifeCycle() {
            super("classpath*:spring-life.xml");
        }
        
        @Test
        public void test(){
            super.getBean("beanLifeCycle");
        }
    }
    <?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:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx">
    
        <bean id="beanLifeCycle" class="org.bean.example.BeanLifeCycle" init-method="start" destroy-method="stop"></bean>
    </beans>

     

      方法二


      案例

    package org.bean.example;
    
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    
    public class BeanLifeCycle implements InitializingBean,DisposableBean{
        
        /*public void start(){
            System.out.println("bean start");
        }
        public void stop(){
            System.out.println("bean stop");
        }*/
        
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("bean afterPropertiesSet");
        }
        
        @Override
        public void destroy() throws Exception {
            System.out.println("bean destroy");
        }
    }
    package org.bean.example;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.BlockJUnit4ClassRunner;
    import org.util.test.UnitTestBase;
    
    @RunWith(BlockJUnit4ClassRunner.class)
    public class TestBeanLifeCycle extends UnitTestBase{
        
        public TestBeanLifeCycle() {
            super("classpath*:spring-life.xml");
        }
        
        @Test
        public void test(){
            super.getBean("beanLifeCycle");
        }
    }
    <?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:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" 
        xmlns:tx="http://www.springframework.org/schema/tx">
    
        <bean id="beanLifeCycle" class="org.bean.example.BeanLifeCycle"></bean>
    </beans>

    方法三:直接在xml文件中的beans头中添加两个属性即可---default-init-method="方法名",default-destroy-method="方法名",然后再相应的bean文件中添加属性的定义的方法

    <?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:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        default-init-method="defaultInit" default-destroy-method="defaultDestroy">
    
        <bean id="beanLifeCycle" class="org.bean.example.BeanLifeCycle" ></bean>
    </beans>
    package org.bean.example;
    
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    
    public class BeanLifeCycle {
        
        public void defaultInit(){
            System.out.println("bean defaultInit");
        }
        public void defaultDestroy(){
            System.out.println("bean defaultDestroy");
        }
    }
    package org.bean.example;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.BlockJUnit4ClassRunner;
    import org.util.test.UnitTestBase;
    
    @RunWith(BlockJUnit4ClassRunner.class)
    public class TestBeanLifeCycle extends UnitTestBase{
        
        public TestBeanLifeCycle() {
            super("classpath*:spring-life.xml");
        }
        
        @Test
        public void test(){
            super.getBean("beanLifeCycle");
        }
    }

    注意:

      三个方法的使用可以同时存在,其调用优先级从高到低:实现接口、在Bean中设置init-method、destroy-method属性、在Beans标签头部中添加默认初始化属性(如果存在前面任意一种方法,则该方法就不会执行,并且该方法是设置了所有bean的默认初始化方法)

      

      

  • 相关阅读:
    Microsoft .NET Framework 2.0实现发送邮件(Email)总结
    Microsoft .NET Framework 2.0对文件传输协议(FTP)操作(上传,下载,新建,删除,FTP间传送文件等)实现汇总
    抽象类
    WingIDE 单步调试 Uliweb Python 代码
    Android 4.0 SDK的离线方式安装
    .NET 3.5 中WCF客户端代理性能改进以及最佳实践
    在linux上部署Redmine
    认识jQuery mobile 框架,资源,书籍
    如何使用搜索技巧来成为一名高效的程序员
    Management Console 工具管理类软件通用开发框架(开放源码)
  • 原文地址:https://www.cnblogs.com/myfaith-feng/p/9215142.html
Copyright © 2020-2023  润新知