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的生命周期的时候,这个时候你开始上网找资料,很大概率会看到下面这张图: 先不论这张图上是否全面,但是就说这张图吧,你是不是背了又忘,忘了又背? 究其原因在于, ...
随机推荐
- python16_day19【Django_抽屉项目】
补充ORM块: 1.select_related() # 解决:当有外健,规避多决查询,使用了join. 多次查询变成一次查询 例:UserInfo.objects.all().select_rel ...
- 如何修改opencart的模版适合为mycncart系统使用
如何修改opencart的模版适合为mycncart系统使用 mycncart跟随opencart的最新代码不断进行升级,并改造和不断加入中国特色的功能,因此opencart的模版均不能够拿来直接套用 ...
- 运输层协议--TCP及UDP协议
TCP及UDP协议 按照网络的五层分级结构来看,TCP及UDP位于运输层,故TCP及UDP是运输层协议.TCP协议--传输控制协议UDP协议--用户数据报协议 多路复用及多路分解 图多路复用及多路分解 ...
- TOSCA自动化测试工具--TestSuite Components
TestSuite Components 分了6块
- MVC 4中的前端渲染 @Helper指令
如果我们需要在一个页面或多个页面显示如人民币格式(后台传回来的无¥)¥的格式化.或是对后台数据作如保留小数个数等处理,这些东西经常要用到,特别是一些NULL值的处理,有可能会出错.这时我们可以通过创建 ...
- LSTM-based Encoder-Decoder for Multi-sensor Anomaly Detection
1.主要工作是将机械设备的传感器数据,LSTM-encoder-decoder模型输入正常数据时间序列训练模型,重构时间序列,然后使用异常数据进行测试,产生较高的重构错误,表明时间序列数据为异常的. ...
- goquery常用语法
Find 查找获取当前匹配的每个元素的后代Eq 选择第几个Attr 获取对应的标签属性AttrOr 获取对应的标签属性.这个可以设置第二个参数.获取的默认值 如果获取不到默认调用对应默认值Each 遍 ...
- PostgreSQL 递归查询 (转)
数据库中的数据存在父子关系(单继承,每一条记录只有一个父亲). 如果要查询一条记录以及他的所有子记录,或者要查询一条记录以及他的所有父记录.那么递归查询就再合适不过了.可以简化复杂的SQL语句 现在 ...
- MySQL_解决ERROR 2006 (HY000) at line XX MySQL server has gone away问题
参考:http://www.111cn.net/database/mysql/106911.htm 1.修改mysqld的配置文件my.cnf 调整max_allowed_packet的值,修改为5M ...
- angularjs 整合bootstrap 时间控件
一.引入js <link href="${basePath}/static/plugin/bootstrap/css/bootstrap-datetimepicker.min.css& ...