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’ ...
随机推荐
- centos6 install mplayer(multimedia)
step_1 http://wiki.centos.org/AdditionalResources/Repositories/RPMForge step_2 http://wiki.centos.or ...
- Netbeans7.0完美中文+Consolas字体显示配置(亲测可用)
最近把开发环境从Eclipse迁移到了Netbeans上面.因为Netbeans已经相当优秀,速度快功能也不必Eclipse差,但是一只有 一个问题一直让我对eclipse非常纠结:如果把字体选择为C ...
- iOS应用审核的通关秘籍
磨刀不误砍柴工.作为手机应用开发者,你需要向应用商店提交应用审核,迅速通过审核可以让你抢占先机.对苹果iOS应用开发者来说尤其如此.苹果应用商店的审核近乎吹毛求疵,下面这些清单可以让你知道苹果会在哪些 ...
- python tornado+mongodb的使用
tornado tar xvzf tornado-1.2.1.tar.gz cd tornado-1.2.1 python setup.py build sudo python setup.py in ...
- LINQ里的Distinct()
IQueryable 继承自IEnumerable 先举例: #region linq to object List<People> peopleList = new List<Pe ...
- android开发之应用Crash自动抓取Log_自动保存崩溃日志到本地
http://blog.csdn.net/jason0539/article/details/45602655 应用发生crash之后要查看log,判断问题出在什么地方,可是一旦应用发布出去,就要想办 ...
- [Twisted] Test
由于Twisted程序采用事件驱动,并使用Deferred来处理事件,使用Python unittest的写测试并不容易.因此, Twisted拓展了unitest,并使用命令行工具来运行测试.这些组 ...
- .Net Framework 开发Http协议
一.Http的基本原理 1.HTTP协议的运作方式 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HTTP协议是基于请求/响应范式的.一个客户机与服务器建立连接后,发送一个请求 ...
- java新手笔记2 数据类型
1.注释 /** doc注释 * 类说明信息 */ //声明类 文件名与类名一致 public class World {//类定界符 //声明方法 main方法 public static void ...
- js【输入一个日期】返回【当前12个月每月最后一天】
Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + ...