(一)scope=“singleton”

知识点:无论获取多少个bean,得到的总是一样的地址,singleton范围下只会创建一个bean实例

1.Bean4.java

package com.inspur.scope;

public class Bean4 {

}

2.InstanceTest.java

package com.inspur.scope;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.inspur.static_factory.Bean2; public class InstanceTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String xmlPath = "com/inspur/scope/beans4.xml";
//2.ApplicationContext 在加载文件时,对bean实例化
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
Bean4 bean4 = (Bean4) applicationContext.getBean("bean4");
System.out.println(bean4);
Bean4 bean5 = (Bean4) applicationContext.getBean("bean4");
System.out.println(bean5); } }

3.bean4.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bean4" class="com.inspur.scope.Bean4" scope="singleton"></bean>
</beans>

4.此时的运行结果是:

5.如果将更改为prototype则获得两个不同的值。

运行结果如下:

最新文章

  1. WiFi QC 自动测试:ixChariot API初探
  2. Android selecter背景选择器使用
  3. HubbleDotNet 的注册码生成器
  4. API -- java.lang.Integer
  5. spring缓存
  6. web.xml的深入学习
  7. Android --ListView分页
  8. Field+offset(len)
  9. Hadoop学习记录(6)|Eclipse安装Hadoop 插件
  10. AndroidRichText 让Textview轻松的支持富文本(图像ImageSpan、点击效果等等类似QQ微信聊天)
  11. head first c&amp;lt;11&amp;gt;初探网络编程上
  12. Fliptile
  13. iOS6和iOS7代码的适配(1)
  14. SaltStack 的远程执行机制
  15. Echarts使用Ajax异步获得数据的前端json格式转化问题
  16. redis : 桌面管理工具 redis-desktop-manager使用指南
  17. Python笔记:Python中is和==的区别
  18. 《GPU高性能编程CUDA实战》第十一章 多GPU系统的CUDA C
  19. C#语法之匿名函数和Lambda表达式
  20. c#基础学习(0629)之导出Excel方法

热门文章

  1. 如何解决DuplicateFileException: Duplicate files copied in APK问题
  2. 给 ecplise 配置struts2配置环境
  3. POJ 2456 Aggressive cows---二分搜索法
  4. [洛谷P1541] 乌龟棋
  5. swift 之嵌套的理解 func chooseStepFunction(backwards: Bool) -&gt; (Int) -&gt; Int
  6. Swift 枚举(七)
  7. Jenkins安装配置过程及问题详解
  8. Java坦克大战 (一) 之产生一个窗口
  9. git应用基础配置
  10. 【C++】隐式类型转换和 explicit关键字