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="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的生命周期的更多相关文章
- JAVA面试题:Spring中bean的生命周期
Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...
- Spring中Bean的生命周期及其扩展点
原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6106456.html Spring中Bean的管理是其最基本的功能,根据下面的图来了解Spr ...
- 简:Spring中Bean的生命周期及代码示例
(重要:spring bean的生命周期. spring的bean周期,装配.看过spring 源码吗?(把容器启动过程说了一遍,xml解析,bean装载,bean缓存等)) 完整的生命周期概述(牢记 ...
- 面试Spring之bean的生命周期
找工作的时候有些人会被问道Spring中Bean的生命周期,其实也就是考察一下对Spring是否熟悉,工作中很少用到其中的内容,那我们简单看一下. 在说明前可以思考一下Servlet的生命周期:实例化 ...
- 一分钟掌握Spring中bean的生命周期!
Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean 的别名只能维持 ...
- Spring中bean的生命周期!
Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...
- Spring中 bean的生命周期
为什么要了解Spring中 bean的生命周期? 有时候我们需要自定义bean的创建过程,因此了解Spring中 bean的生命周期非常重要. 二话不说先上图: 在谈具体流程之前先看看Spring官方 ...
- Spring官网阅读(十)Spring中Bean的生命周期(下)
文章目录 生命周期概念补充 实例化 createBean流程分析 doCreateBean流程分析 第一步:factoryBeanInstanceCache什么时候不为空? 第二步:创建对象(crea ...
- 如果你每次面试前都要去背一篇Spring中Bean的生命周期,请看完这篇文章
前言 当你准备去复习Spring中Bean的生命周期的时候,这个时候你开始上网找资料,很大概率会看到下面这张图: 先不论这张图上是否全面,但是就说这张图吧,你是不是背了又忘,忘了又背? 究其原因在于, ...
随机推荐
- 聚类之k-means
1.介绍 k-means算法以k为参数(所期望的簇的个数),把n个对象分成k个簇(单层划分),用质心(数据点的平均值)定义簇的原型.使得簇内具有较高的相似度,而簇间的相似度较低. 通过聚类,我们能够发 ...
- [笔记] Python实现全排列算法
所谓全排列,就是给定数组,将所有的可能排列组合都枚举出来,n个元素共有n!种排列组合. 举例,对于['1', '2', '3'],全排列结果为:123,132,213,231,312,321,共有3! ...
- 404 Not Found 探秘Nginx转发处理流程
一.问题描述 访问一个链接地址后报404 Not Found nginx/1.10.2 1 112.95.211.154 - - [08/Mar/2018:15:22:21 +0800] " ...
- JavaScript-dom3 json_str dom元素控制 模拟百度搜索
访问关系-封装代码 html <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- 关于Socket和ServerSocket类详解
Socket类 套接字是网络连接的一个端点.套接字使得一个应用可以从网络中读取和写入数据.放在两个不同计算机上的两个应用可以通过连接发送和接受字节流.为了从你的应用发送一条信息到另一个应用,你需要知道 ...
- $ 一步一步学Matlab(2)——Matlab基本通用操作
在上一篇中对Matlab做了一个初步的了解,本文继续来零距离亲身体验Matlab,来感受一下Matlab的一些基本.通用的操作. 命令行窗口 一打开Matlab就能看到命令行窗口,在我所用的这个精简版 ...
- php 中处理 websocket
http://www.cnblogs.com/hustskyking/p/websocket-with-php.html 下面我画了一个图演示 client 和 server 之间建立 websock ...
- jquery获取select选中项的文本
使用jquery获取选中的值很简单 $("#select").val(); 但是获取选中的文本就没有这么直接了 $("#select").find(" ...
- vsftpd基于mysql的认证方式
安装epel源: cd /etc/yum.repos.d wget http://mirrors.neusoft.edu.cn/epel/epel-release-latest-6.noarch.rp ...
- php7不支持curl
百度出来的东西没有一个有用的 终极解决方案: 1.将extension=curl前的分号去掉: 2.将php目录下的libssh2.dll放到apache安装目录的bin目录下 3.重启apache ...