码迷,mamicode.com
首页 > 编程语言 > 详细

spring配置bean的生命周期

时间:2014-11-04 16:53:56      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   color   sp   strong   文件   div   on   

配置文件:

<?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="1"></property>

      <property name="name" value="lijun"></property>

      <property name="password" value="199192"></property>

   </bean>

  

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

      <property name="id" value="1"></property>

      <property name="name" value="lijun"></property>

      <property name="password" value="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的生命周期

标签:style   http   io   color   sp   strong   文件   div   on   

原文地址:http://www.cnblogs.com/fabulousyoung/p/4073877.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!