scope的范围
(一)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则获得两个不同的值。
运行结果如下:
scope的范围的更多相关文章
- Javascript 的执行环境(execution context)和作用域(scope)及垃圾回收
执行环境有全局执行环境和函数执行环境之分,每次进入一个新执行环境,都会创建一个搜索变量和函数的作用域链.函数的局部环境不仅有权访问函数作用于中的变量,而且可以访问其外部环境,直到全局环境.全局执行环境 ...
- 理解nodejs模块的scope
描述 原文档地址:https://docs.npmjs.com/misc/scope 所有npm模块都有name,有的模块的name还有scope.scope的命名规则和name差不多,同样不能有ur ...
- dagger2系列之Scope
Dagger的Scope注解代表的是作用域,通过实现自定义@Scope注解,标记当前生成对象的使用范围,标识一个类型的注射器只实例化一次,在同一个作用域内,只会生成一个实例, 然后在此作用域内共用一个 ...
- scope.$apply是干嘛的
开始用angular做项目的时候,一定碰到过$scope.$apply()方法,表面上看,这像是一个帮助你进行数据更新的方法,那么,它为何存在,我们又该如何使用它呢. JavaScript执行顺序 J ...
- (转)构建自己的AngularJS,第一部分:Scope和Digest
原翻译链接:https://github.com/xufei/Make-Your-Own-AngularJS/edit/master/01.md 原文链接:http://teropa.info/blo ...
- [Android]使用Dagger 2依赖注入 - 自定义Scope(翻译)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5095426.html 使用Dagger 2依赖注入 - 自定义 ...
- AngularJS Scope(作用域)
1. AngularJS Scope(作用域) Scope(作用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带. Scope 是一个对象,有可用的方法和属性. Sc ...
- 4种scope方法
默认作用域,自动加载: default_scope { order(created_at: :desc) } model 调用 find_2时才运行 scope :find_2, ->{ whe ...
- angularjs $scope.$watch(),监听值得变化
myApp.controller('firstController',function($scope,$interval){ $scope.date = new Date(); setInterval ...
- angularjs $scope.$apply 方法详解
myApp.controller('firstController',function($scope,$interval){ $scope.date = new Date(); setInterval ...
随机推荐
- jquery中:input和input的区别
:input表示选择表单中的input,select,textarea,button元素, input仅仅选择input元素. <button>和<input type=" ...
- linux+GraphicsMagick 安装
转摘自:http://blog.csdn.net/fhqsse220/article/details/12995763 GraphicsMagick 安装 下载软件:download:ftp://ft ...
- 记另类Request method 'GET' not supported
一般遇到Request method 'GET' not supported这种问题,大家都会找相应controller下的具体方法,把get改为post之类.但是我这次是在访问静态资源,static ...
- 【BZOJ3942】Censoring [KMP]
Censoring Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 有一个S串和一个T串,长 ...
- 51nod数字1的数量
这道题瞎jbyy了很久 方法可能很奇怪... #include<cstdio> #include<cstring> #include<algorithm> #inc ...
- float/文档流
float : left | right | none | inherit; 文档流是文档中可显示对象在排列时所占用的位置. 浮动的定义: 使元素脱离文档流,按照指定方向发生移动,遇到父级边界或者相邻 ...
- Python标准库——collections模块的Counter类
1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...
- mysql内连接、左连接、右连接举例说明
如下: CREATE TABLE tb ( id INT PRIMARY KEY, NAME VARCHAR (20) ) ; CREATE TABLE ta ( id INT PRIMARY KEY ...
- 【转】Spring MVC 解读——<mvc:annotation-driven/>
转载自:http://my.oschina.net/HeliosFly/blog/205343 一.AnnotationDrivenBeanDefinitionParser 通常如果我们希望通过注解的 ...
- python排序sorted与sort比较
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. sorted(iterable,key=None,revers ...