SPRING IN ACTION 第4版笔记-第二章-004-Bean是否单例
spring的bean默认是单例,加载容器是会被化,spring会拦截其他再次请求bean的操作,返回spring已经创建好的bean.
It appears that the CompactDisc is provided by calling sgtPeppers , but that’s not
exactly true. Because the sgtPeppers() method is annotated with @Bean , Spring will
intercept any calls to it and ensure that the bean produced by that method is returned
rather than allowing it to be invoked again.
For example, suppose you were to introduce another CDPlayer bean that is just
like the first:
@Bean
public CompactDisc sgtPeppers() {
return new SgtPeppers();
} @Bean
public CDPlayer cdPlayer() {
return new CDPlayer(sgtPeppers());
}
@Bean
public CDPlayer anotherCDPlayer() {
return new CDPlayer(sgtPeppers());
}
If the call to sgtPeppers() was treated like any other call to a Java method, then each
CDPlayer would be given its own instance of SgtPeppers . That would make sense if we
were talking about real CD players and compact discs. If you have two CD players,
there’s no physical way for a single compact disc to simultaneously be inserted into
two CD players.
In software, however, there’s no reason you couldn’t inject the same instance of
SgtPeppers into as many other beans as you want. By default, all beans in Spring are
singletons, and there’s no reason you need to create a duplicate instance for the sec-
ond CDPlayer bean. So Spring intercepts the call to sgtPeppers() and makes sure
that what is returned is the Spring bean that was created when Spring itself called
sgtPeppers() to create the CompactDisc bean. Therefore, both CDPlayer beans will
be given the same instance of SgtPeppers
SPRING IN ACTION 第4版笔记-第二章-004-Bean是否单例的更多相关文章
- SPRING IN ACTION 第4版笔记-第二章-003-以Java形式注入Bean、@Bean的用法
1. package soundsystem; import org.springframework.context.annotation.Bean; import org.springframewo ...
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource
1. package soundsystem; import org.springframework.beans.factory.annotation.Autowired; public class ...
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-006-当构造函数有集合时的注入
一.当构造函数有集合时,只能用<CONSTRUCTOR-ARG>,不能用C-NAMESPACE 二. 1. package soundsystem.collections; import ...
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在XML配置文件中引入JAVA配置文件 <import> 、<bean>
一.在xml中引入xml,用<import> <?xml version="1.0" encoding="UTF-8"?> <be ...
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-007-以set方法注入<property>\p-namespace\util-space
一.注入简单属性 package soundsystem.properties; import org.springframework.beans.factory.annotation.Autowir ...
- SPRING IN ACTION 第4版笔记-第二章Wiring Beans-005-<constructor-arg>和c-namespace
1. package soundsystem; public class SgtPeppers implements CompactDisc { private String title = &quo ...
- SPRING IN ACTION 第4版笔记-第二章-002-@ComponentScan、@Autowired的用法
一.@ComponentScan 1. @Configuration //说明此类是配置文件 @ComponentScan //开启扫描,会扫描当前类的包及其子包 public class CDPla ...
- SPRING IN ACTION 第4版笔记-第二章-001-用@Autowired\@ComponentScan、@Configuration、@Component实现自动装载bean
1. package soundsystem; import org.springframework.context.annotation.ComponentScan; import org.spri ...
- SPRING IN ACTION 第4版笔记-第一章-005-Bean的生命周期
一. 1. As you can see, a bean factory performs several setup steps before a bean is ready touse. Let’ ...
随机推荐
- Windows redis集群搭建
一.Windows下Redis安装 下载地址https://github.com/dmajkic/redis/downloads,下载到的Redis支持32bit和64bit.根据自己实际情况选择,本 ...
- 获取当前时间日期并格式化--JS
工作当中,总是遇到很多觉得不错的JS脚本.现在觉得还是找个地方记录下来,以后可以随时查看. /** *获取当前时间日期并格式化 */ function getNowDate(){ var mydate ...
- Verilog HDL常用的行为仿真描述语句
一.循环语句 1.forever语句 forever语句必须写在initial模块中,主要用于产生周期性波形. 2.利用for.while循环语句完成遍历 for.while语句常用于完成遍历测试.当 ...
- UML类图细节
类图表述的是类和类之间的关系,是前期用来推演代码设计,后期用来优化代码的结构,寻找设计不合理之处.网上解释类图和类图的画法,往往表述的十分晦涩,或者例子呆板,很难具体的操作. 类图一般在详细设计过程中 ...
- .net中压缩和解压缩的处理
最近在网上查了一下在.net中进行压缩和解压缩的方法,方法有很多,我找到了以下几种: 1.利用.net自带的压缩和解压缩方法GZip 参考代码如下: //======================= ...
- js判断手机浏览器操作系统和微信浏览器的方法
做手机端的前端开发,少不了对手机平台的判断.如,对于app下载,就要判断在Android平台下就显示Android下载提示:在iOS平台下就显示iOS下载提示. 今天就为大家介绍一下用js判断手机客户 ...
- 关于添加非系统framework后,import导入头文件时没有提示的解决办法
##1.选择target(就是左边你的工程target)—— BuildSettings —— search Paths 下的 User Header Search Paths(如图所示: ##2.双 ...
- js控制文本框输入数字和小数点等
1.文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace(/\D/g,'')" onafte ...
- 对REST的一些理解
昨天学习REST,发现有篇文章写的真心不错,看了一遍,并没有完全理解,将一些感觉比较重要的做个记录. 文章链接:REST简介 定义 Representational State Transfer ( ...
- 16_MyBatis中期小结
[MyBatis是什么] MyBatis是一个持久层框架,Mybatis是一个不完全的ORM框架,SQL语句需要程序员自己去编写,但是MyBatis也有映射(输入参数映射.输出结果映射). MyBat ...