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是否单例的更多相关文章

  1. SPRING IN ACTION 第4版笔记-第二章-003-以Java形式注入Bean、@Bean的用法

    1. package soundsystem; import org.springframework.context.annotation.Bean; import org.springframewo ...

  2. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource

    1. package soundsystem; import org.springframework.beans.factory.annotation.Autowired; public class ...

  3. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-006-当构造函数有集合时的注入

    一.当构造函数有集合时,只能用<CONSTRUCTOR-ARG>,不能用C-NAMESPACE 二. 1. package soundsystem.collections; import ...

  4. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在XML配置文件中引入JAVA配置文件 <import> 、<bean>

    一.在xml中引入xml,用<import> <?xml version="1.0" encoding="UTF-8"?> <be ...

  5. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-007-以set方法注入<property>\p-namespace\util-space

    一.注入简单属性 package soundsystem.properties; import org.springframework.beans.factory.annotation.Autowir ...

  6. SPRING IN ACTION 第4版笔记-第二章Wiring Beans-005-<constructor-arg>和c-namespace

    1. package soundsystem; public class SgtPeppers implements CompactDisc { private String title = &quo ...

  7. SPRING IN ACTION 第4版笔记-第二章-002-@ComponentScan、@Autowired的用法

    一.@ComponentScan 1. @Configuration //说明此类是配置文件 @ComponentScan //开启扫描,会扫描当前类的包及其子包 public class CDPla ...

  8. SPRING IN ACTION 第4版笔记-第二章-001-用@Autowired\@ComponentScan、@Configuration、@Component实现自动装载bean

    1. package soundsystem; import org.springframework.context.annotation.ComponentScan; import org.spri ...

  9. 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’ ...

随机推荐

  1. euqals和hashcode

    hashcode这个方法是用来鉴定2个对象是否相等的. 那你会说,不是还有equals这个方法吗? 不错,这2个方法都是用来判断2个对象是否相等的.但是他们是有区别的. 一般来讲,equals这个方法 ...

  2. hibernate篇章六--demo(0.准备工作)

    这个demo是学习自:学习不可已 这个章节我们先来说准备工作:Hibernate之第〇解之准备工作 首先: 1.新建java project--hibernate_demo_1: 2.新建src fo ...

  3. Unity3D 之3D游戏入门Hello world(一)

    这几天开始玩Unity3D 有关3D的内容了,去年开始玩过一段时间的2D制作,不过因为年初找工作,所以放了一段时间, 现在再捡起来发现忘的已经差不多了,只能再从头开始,所以就从3D开始算了.下面是3D ...

  4. Help View修复

    好吧,手贱把ProgramData里关于Help View的某些数据删除了 (在任何情况下都不要删除此文件夹中的任何数据).即使卸载后重新安装也出现错误,可以参考的http://social.msdn ...

  5. Java_类和对象(完美总结)_转载_覆盖和隐藏的区别,覆盖就不能使用了,而隐藏提供全局方法名或者全局变量名还可以使用

    转载自海子:http://www.cnblogs.com/dolphin0520/p/3803432.html Java:类与继承 对于面向对象的程序设计语言来说,类毫无疑问是其最重要的基础.抽象.封 ...

  6. CAS Server 单点登录开源项目

    https://www.apereo.org/cas/download This project contains code that can extent an existing ASP.NET w ...

  7. iOS-事务相关

    事务管理 事务(Transaction):1.构成单一逻辑工作单元的操作集合DBMS中的用户程序DBMS外的可执行程序对数据库的读/写操作序列2.读从数据库中读取数据,首先从磁盘中读到内存(Buffe ...

  8. swift-08-使用键值对儿统计字符在字符串中出现的次数

    // //  main.swift //  12- // //  Created by wanghy on 15/8/9. //  Copyright (c) 2015年 wanghy. All ri ...

  9. asp.net:验证控件中ValidationExpression的写法

    手机号:"\d{11}"传真号:"\d{7,12}" EMAIL: ".{2,15}@.{2,15}\..{2,4}" 邮箱正则表达式:/^ ...

  10. 一些简单的帮助类(2)-- JavaSctipt Array Linq

    在日程工作中经常会遇到这样的问题 一个JS数组 我们要找出其中 一些符合要求的类容 又或者对数组里的类容求和求平均数之类的一般的做法是循环里面的类容做判断添加到一个新的集合里 var array = ...