配置文件:

<?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的生命周期的更多相关文章

  1. JAVA面试题:Spring中bean的生命周期

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...

  2. Spring中Bean的生命周期及其扩展点

    原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6106456.html Spring中Bean的管理是其最基本的功能,根据下面的图来了解Spr ...

  3. 简:Spring中Bean的生命周期及代码示例

    (重要:spring bean的生命周期. spring的bean周期,装配.看过spring 源码吗?(把容器启动过程说了一遍,xml解析,bean装载,bean缓存等)) 完整的生命周期概述(牢记 ...

  4. 面试Spring之bean的生命周期

    找工作的时候有些人会被问道Spring中Bean的生命周期,其实也就是考察一下对Spring是否熟悉,工作中很少用到其中的内容,那我们简单看一下. 在说明前可以思考一下Servlet的生命周期:实例化 ...

  5. 一分钟掌握Spring中bean的生命周期!

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean 的别名只能维持 ...

  6. Spring中bean的生命周期!

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...

  7. Spring中 bean的生命周期

    为什么要了解Spring中 bean的生命周期? 有时候我们需要自定义bean的创建过程,因此了解Spring中 bean的生命周期非常重要. 二话不说先上图: 在谈具体流程之前先看看Spring官方 ...

  8. Spring官网阅读(十)Spring中Bean的生命周期(下)

    文章目录 生命周期概念补充 实例化 createBean流程分析 doCreateBean流程分析 第一步:factoryBeanInstanceCache什么时候不为空? 第二步:创建对象(crea ...

  9. 如果你每次面试前都要去背一篇Spring中Bean的生命周期,请看完这篇文章

    前言 当你准备去复习Spring中Bean的生命周期的时候,这个时候你开始上网找资料,很大概率会看到下面这张图: 先不论这张图上是否全面,但是就说这张图吧,你是不是背了又忘,忘了又背? 究其原因在于, ...

随机推荐

  1. 聊一聊 Django 中间件

    Django默认的Middleware有七个: MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.cont ...

  2. 爬虫——请求库之selenium模块

      阅读目录 一 介绍 二 安装 三 基本使用 四 选择器 五 等待元素被加载 六 元素交互操作 七 其他 八 项目练习 一 介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解 ...

  3. TWebBrowser: Determine when a page with Frames is completed

    TWebBrowser: Determine when a page with Frames is completed 6 comments. Current rating: (3 votes). L ...

  4. [React-Native]入门(Hello World)

    (1)需要一台Mac(OSX),这个是前提,建议还是入手一本啦. (2)在Mac上安装Xcode,建议Xcode 6.3以上版本 (3)安装node.js:https://nodejs.org/dow ...

  5. Castle连接多数据库配置

    ActiveRecord 的多数据库配置基本沿袭了 NHibernate 的思想,只不过在配置文件结构上作了些调整.1. 采用继承方式,归纳使用同一数据库的类型.比如 A.B.C.D.E 中 A.B连 ...

  6. Mybatis中的#与$的区别

    一.对比场景 场景:数据库分表时,需要将分表的表序号传入的sql中. SpringBoot中使用注解如下: @Insert("insert into collect_#{tblNum}(id ...

  7. ubuntu 16.4安装卸载apache+php+mysql

    1.安装apache sudo apt-get update sudo apt-get install apache2 2.安装php5.6 添加PPA源:add-apt-repository ppa ...

  8. linux ftp 简单搭建

    1.安装 yum install vsftpd 2.重启服务 /sbin/service vsftpd restartShutting down vsftpd: [ OK ]Starting vsft ...

  9. 20145303 《Java程序设计》第六周学习总结

    20145303 <Java程序设计>第六周学习总结 教材学习内容总结 第十章:输入/输出 (InputStream与OutputStream) 1.Java将输入/输出抽象化为串流,数据 ...

  10. uboot下ext4load的用法

    将sd卡的某个分区下的某个目录里的某个文件加载到内存的某个地址,示例如下: ext4load mmc 0:1 0xa0000000 /bin/vi