配置文件:

<?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. CSS 换行知多少: word-wrap && word-break && white-space && word-spacing

    word-wrap : 首先提一下,word-wrap 这个 CSS 属性在CSS3中已经被更名为 overflow-wrap,这样语义化也是为了避免与 word-break 混淆: Referenc ...

  2. C# 获取计算机cpu 硬盘 网卡信息

    /// <summary>/// 机器码         /// </summary>       public class MachineCode         {     ...

  3. 阿里云搭建go开发环境

    开通了一个阿里云来玩,记录一下环境搭建的过程 运行环境 ECS Ubuntu 16.04 64位 过程 #切换到安装文件夹 cd /usr/local #下载go #由于墙的原因,直接下载官方的可能会 ...

  4. MyBatisPartA

    (正在补充) 1.从第一个程序开始,通过mybatis实现数据库表内容的增删改查 (源码zip包) 1.0准备工作 建数据库mybatis,在其中创建表sql语句如下: ; -- ---------- ...

  5. MySQL 温故知心(二) 事务的隔离级别

    事务的隔离级别 A事务做了操作 没有提交 对B事务来说 就等于没做 获取的都是之前的数据但是 在A事务中查询的话 查到的都是操作之后的数据没有提交的数据只有自己看得到,并没有update到数据库 查看 ...

  6. 54. Spiral Matrix(矩阵,旋转打印)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  7. php 输出 sql语句

    第一种方法 $data = M('news')->field("title,date_format(postdate,'%Y-%m-%d') as postdate,content&q ...

  8. Delphi7编译时,发生Access violation at address 00A7B628 in module 'dcc70.dll'. Read of address 00000000.(Delphi6升级到Delphi7)

    最近接了一个项目,要求使用Delphi7来开发程序,可是由于我们之前均使用delphi6来开发程序的,而且Delphi6使用很长时间了,积累并改造了第三方控件很成熟了: 故把Delphi6的控件移植( ...

  9. 你可能不熟悉的JS总结

    暂时性死区 只要块级作用域存在let命令,它所声明的变量就"绑定"这个区域,不再受外部的影响.这么说可能有些抽象,举个例子: var temp = 123; if(true) { ...

  10. kafka监控搭建

    1. 下载 wget https://github.com/quantifind/KafkaOffsetMonitor/releases/download/v0.2.1/KafkaOffsetMoni ...