zoukankan      html  css  js  c++  java
  • Spring 新手教程(二) 生命周期和作用域


    以下的知识点是针对使用配置文件讲的(annotation相应标签后面文章会具体阐述)


    Bean的生命周期:

     

    为什么关心这个问题?

    我们使用IOC的目的就是为了让Spring IOC帮我们管理对象。从而不须要去自己手动去new 。何时new,怎么管理对象间依赖,什么时候销毁等等非常多问题。而对象在Spring容器看来就是一个个bean,理所当然要理解Bean的生命周期。

     

    生命周期的过程有哪些?

    定义、初始化、使用、销毁。


    初始化:(1)实现org.springframework.beans.factory.InitlialzingBean接口。并覆盖当中的afterPropertiesSet方法;

           ( 2)配置init-method;

    销毁:(1)实现org.springframework.beans.factory.DisposableBean接口,并覆盖destory方法;

                       (2)配置destroy-method;

     

    当然也能够使用配置默认方式(看以下Spirng对应配置文件):

    default-init-method="defautInit"   default-destroy-method="defaultDestroy


    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    
    //实际应用中。须要特别注意的是。以下三种初始化和销毁的方法仅仅须要选用一种就可以,继承接口的那种不须要再配置文件里声明。而其它两种则须要在配置文件里声明
    
    //这里是实现了相应的接口,可是注意要覆盖相应的方法
    public class BeanLifeCycle implements InitializingBean, DisposableBean {
    	
    	
    	// default-init-method="defautInit" default-destroy-method="defaultDestroy" 配置文件里的默认构造方法
    	public void defautInit() {
    		System.out.println("Bean defautInit.");
    	}
    	
    	// default-init-method="defautInit" default-destroy-method="defaultDestroy" 配置文件里的默认构造方法
    	public void defaultDestroy() {
    		System.out.println("Bean defaultDestroy.");
    	}
    
    	@Override    
    	//覆盖接口要求的方法
    	public void destroy() throws Exception {
    		System.out.println("Bean destroy.");
    	}
    
    	@Override
    	//覆盖接口要求的方法
    	public void afterPropertiesSet() throws Exception {
    		System.out.println("Bean afterPropertiesSet.");
    	}
    	
    	//还有一种方法。在配置文件里规定的 init-method="start" destroy-method="stop"  相应类中声明相应方法
    	public void start() {
    		System.out.println("Bean start .");
    	}
    	
    	//还有一种方法。在配置文件里规定的 init-method="start" destroy-method="stop"  相应类中声明相应方法
    	public void stop() {
    		System.out.println("Bean stop.");
    	}
    	
    }
    



    这里须要注意几个关系(1)无论是使用默认的初始化和销毁方法,还是继承接口,或者在配置文件里声明,都须要在对应的类中写上对应的方法

    当然,针对更一般的类使用默认设置有灵活性,能够在不同文件里仅仅要改写对应的方法就可以,而不要每次都配置,或者都去实现接口。(2)假设我们三种方法同一时候使用,那么, default-init-method="defautInit"default-destroy-method="defaultDestroy"不起作用。(3)在详细的类中能够不实现默认的详细的方法(仅仅是不报错,可是不起初始化、销毁作用)。可是。假设是继承接口或自己配置,那么在详细类中一定要实现对应方法;




    Bean的作用域:

     

    为什么关心这个问题?

    同上面生命周期一样,我们必需要关心,Bean在哪些地方能用。不同的http请求来时创建的是不是同一个Bean等问题。而这些直接跟我们开发相关。

     

    Bean的作用域几种类型:

    single:单例。一个Bean容器仅仅存在一份;

    prototype:每次请求都创建新的实例;

    request:每次http请求时创建一个实例,并仅在当前的request内生效;

    session:每次http请求时创建一个实例,并仅在当前的session内生效;


    <?xml version="1.0" encoding="UTF-8"?

    > <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd" > <!-- 一个web项目能够有许多的spring配置文件,可是这些文件得自己在须要载入对象的时候读取配置文件 scope="singleton,注意这边配置--> <bean id="beanScope" class="com.xidian.BeanScope" scope="singleton"></bean> </beans>


    非常明显,不同的scope配置,对应的使用效果肯定不同,这要视详细开发要求而定。





    你看会了吗?欢迎讨论 http://blog.csdn.net/code_7/



  • 相关阅读:
    前端与算法 leetcode 344. 反转字符串
    JavaScript闭包使用姿势指南
    前端与算法 leetcode 48. 旋转图像
    前端与算法 leetcode 36. 有效的数独
    前端与算法 leetcode 1. 两数之和
    前端与算法 leetcode 283. 移动零
    前端与编译原理 用js去运行js代码 js2run
    前端与算法 leetcode 66. 加一
    前端与算法 leetcode 350. 两个数组的交集 II
    前端与算法 leetcode 26. 删除排序数组中的重复项
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7269226.html
Copyright © 2011-2022 走看看