首页 > 代码库 > spring配置bean的生命周期

spring配置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:context="http://www.springframework.org/schema/context"

   xmlns:tx="http://www.springframework.org/schema/tx"

   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"

   default-lazy-init="false">

   <!-- default-lazy-init用于指定所有的单例对象默认初始化的时机,默认为false -->

 

   <!-- scope属性用于指定bean的生命周期,

    singleton表示单例,每次ac.getBean()返回的都是同一个对象,

     prototype表示原型/多例,每次ac.getBean()都会生成新的对象,

          默认为单例 -->

         

    <!-- lazy-init属性表示什么时候开始创建单例对象,只对单例有效,

     true表示在需要创建对象的时候才开始创建,

     false表示在容器启动的时候就开始创建对象,

          默认与default-lazy-init的值一样 -->

         

   <bean id="user1" class="com.colorlight.springscope.User" scope="singleton"

      lazy-init="true">

      <property name="id" value=http://www.mamicode.com/"1"></property>

      <property name="name" value=http://www.mamicode.com/"lijun"></property>

      <property name="password" value=http://www.mamicode.com/"199192"></property>

   </bean>

  

   <bean id="user2" class="com.colorlight.springscope.User" scope="prototype">

      <property name="id" value=http://www.mamicode.com/"1"></property>

      <property name="name" value=http://www.mamicode.com/"lijun"></property>

      <property name="password" value=http://www.mamicode.com/"199192"></property>

   </bean>

</beans>

 

User类:

 

package com.colorlight.springscope;

 

public class User {

   private int id;

   private String name;

   private String password;

  

   public User(){

      System.out.println("创建了User的实例!");

   }

  

   public int getId() {

      return id;

   }

   public void setId(int id) {

      this.id = id;

   }

   public String getName() {

      return name;

   }

   public void setName(String name) {

      this.name = name;

   }

   public String getPassword() {

      return password;

   }

   public void setPassword(String password) {

      this.password = password;

   }

}

 

测试类:

 

package com.colorlight.springscope;

 

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class MainTest {

 

   ApplicationContext ac = new ClassPathXmlApplicationContext(

        "applicationContext.xml"this.getClass());

 

   // 单例

   @Test

   public void test1() {

      System.out.println("在getBean()调用之前");

      User user1 = (User) ac.getBean("user1");

      User user2 = (User) ac.getBean("user1");

      System.out.println(user1 != null);

      System.out.println(user1 == user2);

   }

 

   // 多例

   @Test

   public void test2() {

      System.out.println("在getBean()调用之前");

      User user1 = (User) ac.getBean("user2");

      User user2 = (User) ac.getBean("user2");

      System.out.println(user1 != null);

      System.out.println(user1 == user2);

   }

}

 

spring配置bean的生命周期