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’ ...
随机推荐
- (转) ASP.NET页面缓存
原文:http://www.cnblogs.com/Sky_KWolf/archive/2010/12/05/1897158.html 静态页面全部内容保存在服务器内存中.当再有请求时,系统将缓存中的 ...
- 20160418javaweb之 Filter过滤器
Servlet规范中 Servlet Listener Filter 1.开发Filter 想要开发一个过滤器需要如下两个步骤: (1)写一个类实现特定的接口Filter 生命周期:当服务器启动时,w ...
- java web -部署在linux
概述: 初次将java web项目部署到linux上, 还是很顺利的, 基本上没有什么错误. 步骤: 1, 安装jdk(官网中说了很清晰了),在linux上安装安装jdk, 不想windows那样, ...
- windows8 8.1 安装完 ubuntu无法挂载 ntfs分区 解决方法
windows8 8.1 安装完 ubuntu无法挂载 ntfs分区 解决方法: 最近安装完发现8.1系统后,ubuntu无法加载以前的ntfs分区了,特别是我添加到了/etc/fstab里面了 导致 ...
- #ifndef#define#endif的用法
在网上看到了感觉作者总结得很好,作者辛苦了! #ifndef#define#endif的用法 文件中的#ifndef 头件的中的#ifndef,这是一个很关键的东西.比如你有两个C文件,这两个C文件都 ...
- linux安装rzsz
rz,sz是Linux/Unix同Windows进行ZModem文件传输的命令行工具优点:比ftp命令方便,而且服务器不用打开FTP服务. sz:将选定的文件发送(send)到本地机器rz:运行该命令 ...
- C#查询当前微信自定义菜单结构
查询 string access_token = "你的token"; string posturl = "https://api.weixin.qq.com/cgi-b ...
- 注释玩转webapi
using System; using System.Collections.Generic; using System.Net.Http.Formatting; using System.Web.H ...
- java连接远程服务器之manyged-ssh2 (windows和linux)
一.所需要的jar包 需要借助Ganymed SSH的jar包: ganymed-ssh2-262.jar 下载地址: http://www.ganymed.ethz.ch/ssh2/ API详情: ...
- bc
调试脚本报错bc: command not found 原因是因为这个linux环境里没有安装计算器工具bc 用 apt-get install bc 或者 yum -y install bc 安装后 ...