scope的范围
2024-10-21 05:41:51
(一)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则获得两个不同的值。
运行结果如下:
最新文章
- WiFi QC 自动测试:ixChariot API初探
- Android selecter背景选择器使用
- HubbleDotNet 的注册码生成器
- API -- java.lang.Integer
- spring缓存
- web.xml的深入学习
- Android --ListView分页
- Field+offset(len)
- Hadoop学习记录(6)|Eclipse安装Hadoop 插件
- AndroidRichText 让Textview轻松的支持富文本(图像ImageSpan、点击效果等等类似QQ微信聊天)
- head first c&;lt;11&;gt;初探网络编程上
- Fliptile
- iOS6和iOS7代码的适配(1)
- SaltStack 的远程执行机制
- Echarts使用Ajax异步获得数据的前端json格式转化问题
- redis : 桌面管理工具 redis-desktop-manager使用指南
- Python笔记:Python中is和==的区别
- 《GPU高性能编程CUDA实战》第十一章 多GPU系统的CUDA C
- C#语法之匿名函数和Lambda表达式
- c#基础学习(0629)之导出Excel方法
热门文章
- 如何解决DuplicateFileException: Duplicate files copied in APK问题
- 给 ecplise 配置struts2配置环境
- POJ 2456 Aggressive cows---二分搜索法
- [洛谷P1541] 乌龟棋
- swift 之嵌套的理解 func chooseStepFunction(backwards: Bool) ->; (Int) ->; Int
- Swift 枚举(七)
- Jenkins安装配置过程及问题详解
- Java坦克大战 (一) 之产生一个窗口
- git应用基础配置
- 【C++】隐式类型转换和 explicit关键字