Spring - IOC创建对象的时机,创建Bean的Scope 及其 IOC的 生命周期

来源:互联网 发布:js视频进度条 编辑:程序博客网 时间:2024/05/17 03:06

一. Spring容器创建Bean的Scope

    spring容器创建bean的scope : 验证创建的对象是单例的还是多例的?

1. 由spring产生的bean默认是单例的;
2. 在spring配置文件中bean的属性scope="singleton/prototype/request/session/global session" 默认就是 singletion
        3. 如果spring的配置文件的scope为prototype,则在得到该bean时(context.getBean)才创建对象;

       4.示例验证 :

    (1)新建类,验证集合 ,如果两个对象操作一个集合,集合长度改变,那么是同一个集合,则为单例模式;

public class HelloScope { public  List<String> lists=new ArrayList<String>();     public void hello(){     System.out.println("Hello bean scope");     }}
 

      (2)bean.xml

<?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-2.5.xsd">     <bean id="HelloScope" class="cn.labelnet.scope.HelloScope"></bean></beans>

     (3) 测试

@Testpublic void testHelloWordFactoryScope(){ApplicationContext applicationContext=new ClassPathXmlApplicationContext(BENA_CONTEXT_XML);HelloScope helloScope=(HelloScope) applicationContext.getBean("HelloScope");helloScope.lists.add("yuan");helloScope.lists.add("an");HelloScope helloScope2=(HelloScope) applicationContext.getBean("HelloScope");System.out.println(helloScope2.lists.size());}

     (4)结果



      (5)修改配置 设置 scope属性为prototype

<?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-2.5.xsd">     <bean id="HelloScope" class="cn.labelnet.scope.HelloScope" scope="prototype"></bean></beans>


      (6)结果



二.  spring容器创建对象的时机 :

    

        1. 条件1: 在单例的情况下

(1) 时机 :
     1). 默认的情况下,启动spring容器时就创建对象了;
      2). 在spring的配置文件中的一个属性lazy-init="default/true/false"
                    default/false: 在启动spring容器的时候创建对象;
true :在context.getBean时就要创建对象;
 (2) 意义:
在默认情况下,启动spring容器的时候,检查spring容器配置文件的正确性,如果再结果tomcat,如果spring容器不能正常启动,整个tomcat就不能启动,但是这样的缺点就是把一些bean放在了内存中,如果有数据,则对内存来是一个消耗;
在为true时,减少内存消耗,但不会检测配置是否有错误;
 

 2. 条件2 :

在多例的情况下,在conext.getBean时才创建对象;


         3.配置 lazy-init属性

 <bean id="HelloScope" class="cn.labelnet.scope.HelloScope" lazy-init="true"></bean>

三. spring容器的生命周期 

 1. spring容器创建对象
 2. 执行init方法
  3. 调用自己的方法
  4. 当spring容器关闭的时候执行destory方法
  

         5. 配置实现:

 1). 初始化 init 对应 bean属性 init-method ;
  2). 销毁 destory 对应 bean属性 destory-method;
  3). spring关闭的时候,销毁对象 (ClassPathXMLApplicationContext 对象有close方法)
  4). 当scope为protitype时,spring容器不调用destory方法

<bean  id="helloInitdestory" class="cn.labelnet.initdestory.HelloInitDestory" init-method="onInit" destroy-method="onDestory" scope="prototype"></bean> 

       

          6.示例

           1)创建类

public class HelloInitDestory {public HelloInitDestory() {      System.out.println("我是构造函数");}public void onInit(){System.out.println("我是初始化操作");}public void hello(){System.out.println("Hello , Spring !");}public void onDestory(){System.out.println("我是销毁操作");}}

      2)创建bean.xml

            init-method属性对应初始化方法,destory-method属性对应销毁方法;

<?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-2.5.xsd">    <bean  id="helloInitdestory" class="cn.labelnet.initdestory.HelloInitDestory" init-method="onInit" destroy-method="onDestory" scope="prototype"></bean>      </beans

    3)测试

private final String APPLICATION_CONTEXT="ApplicationContext.xml";@Testpublic void testHelloInitDestory() {ApplicationContext applicationContext=new ClassPathXmlApplicationContext(APPLICATION_CONTEXT);HelloInitDestory helloInitDestory=(HelloInitDestory) applicationContext.getBean("helloInitdestory");helloInitDestory.hello();ClassPathXmlApplicationContext xmlApplicationContext=(ClassPathXmlApplicationContext)applicationContext; xmlApplicationContext.close();}


    4)结果


   5)更改bean非单例模式

   <bean  id="helloInitdestory" class="cn.labelnet.initdestory.HelloInitDestory" init-method="onInit" destroy-method="onDestory" scope="prototype"></bean> 

      执行后,就没有执行销毁方法;即  当scope为protitype时,spring容器不调用destory方法;

  6)结果


四.Demo免积分下载


http://download.csdn.net/detail/lablenet/9377346


0 0
原创粉丝点击