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的生命周期的时候,这个时候你开始上网找资料,很大概率会看到下面这张图: 先不论这张图上是否全面,但是就说这张图吧,你是不是背了又忘,忘了又背? 究其原因在于, ...
随机推荐
- WKWebkit使用
webkit使用WKWebView来代替IOS的UIWebView和OSX的WebView,并且使用Nitro JavaScript引擎,这意味着所有第三方浏览器运行JavaScript将会跟safa ...
- 数据库(11)-- Hash索引和BTree索引 的区别
索引是帮助mysql获取数据的数据结构.最常见的索引是Btree索引和Hash索引. 不同的引擎对于索引有不同的支持:Innodb和MyISAM默认的索引是Btree索引:而Mermory默认的索引是 ...
- matplotlib.pyplot 让数据可视化
1.条形图 import matplotlib.pyplot as plt plt.style.use('ggplot') # 使用ggplot样式来模拟ggplot2风格的图形,ggplot2是一个 ...
- Python 开发面试总结
网络基础 如何确定发送过来的数据的完整性(有无中间人攻击)? 散列值校验(MD5.SHA-1).数字签名(PGP),需要用户亲自校验,若是散列值或数字签名本身被篡改,用户是无法判断出来的. HTTPS ...
- ansible playbook部署ELK集群系统
一.介绍 总共4台机器,分别为 192.168.1.99 192.168.1.100 192.168.1.210 192.168.1.211 服务所在机器为: redis:192.168.1.211 ...
- selenium+python—实现自动化测试基本思路
测试是一个贯穿于整个开发过程的连续过程,测试最基本的原理就是比较预期结果是否与实际执行结果相同,如果相同则测试成功,否则测试失败. 为了让单元测试代码能够被测试和维护人员更容易地理解,最好的解决办法是 ...
- Web前端学习笔记之jQuery选择器
JQuery过滤器 经过一晚上的查找整理,终于整理出一套应该算最全面的JQuery选择过滤器的方法了.所有代码均经过测试.首先HTML代码 HTML Code <html><head ...
- Python3.x:python: extend (扩展) 与 append (追加) 的区别
Python3.x:python: extend (扩展) 与 append (追加) 的区别 1,区别: append() 方法向列表的尾部添加一个新的元素.只接受一个参数: extend()方法只 ...
- 20145310 《Java程序设计》第1周学习总结
20145310 <Java程序设计>第1周学习总结 教材学习内容总结 第一周主要学习教材前两章的知识.第一章主要学习了java的历史,版本的迁移以及一些相关的专有名词之间的联系和下载安装 ...
- 20145314郑凯杰 《Java程序设计》第4周学习总结
20145314郑凯杰 <Java程序设计>第4周学习总结 所有代码已上传: 教材学习内容总结 ①继承 设计程序中,因们需要设计多个模块,我想到了李晓东以前教我们的三个字"模块化 ...