3.1 scope
- scope描述spring容器是怎么样新建类的实例的(bean);
- 在spring中默认的scope是singleton,这意味着无论你在程序中多少地方使用这个bean,它们都共享唯一个实例;
- spring内置的scope有如下几个:
- singleton:一个spring容器中只有一个bean的实例;
- prototype:每次调用新建一个bean的实例;
- request:web项目中,每一个http request,新建一个bean实例;
- session:web项目中,每一个http session,新建一个bean实例;
- globalSession:这个只在portal应用中有用,每一个global http session,新建一个bean实例
3.2 演示
3.2.1 新建scope为singleton的类
package com.wisely.scope;
import org.springframework.stereotype.Service;
@Service//默认scope为singleton
public class DemoSingletonService {
}
3.2.2 新建scope为prototype的类
package com.wisely.scope;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope("prototype")
public class DemoPrototypeService {
}
3.2.3 测试
package com.wisely.scope;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.scope");
//分别从spring容器里获得两次实例,为singleton时共享一个实例,用prototype是又新建了一个实例
DemoSingletonService s1 = context.getBean(DemoSingletonService.class);
DemoSingletonService s2 = context.getBean(DemoSingletonService.class);
DemoPrototypeService p1 = context.getBean(DemoPrototypeService.class);
DemoPrototypeService p2 = context.getBean(DemoPrototypeService.class);
System.out.println("s1与s2是否相等:"+s1.equals(s2));
System.out.println("p1与p2是否相等:"+p1.equals(p2));
context.close();
}
}
输出
s1与s2是否相等:true
p1与p2是否相等:false
相关推荐
国元点睛-20230825-国元期货-23页
眼睛穴位图-睛明穴-取穴方法 方便经常上网的朋友使用
六爱护眼睛---复式统计图(第1课时).doc
图像与眼睛(2009-04-1.pdf
让消费者眼睛亮起来--稀世宝矿泉水整合营销策划案例(doc 20).doc
包含了谷歌眼睛的全部核心代码,可以用记事本、Dreamweaver、写字板、html等打开
这是一款保护眼睛的小软件,绿色软件,不需安装即可使用,试一下吧。
[最新]八年级上册物理:5.4-眼睛和眼镜-精品教案.pdf
闭着眼睛记单词-英语基础《英语1000高频词汇》
使用OpenCV1.0实现了眼睛状态识别,首先用harr检测眼睛,然后用模板匹配的方法标记眼睛状态。 程序需要使用Eyes45x11.xml Mouth25x15.xml Nose25x15.xml,将其放到C:\Program Files\OpenCV\data\haarcascades目录下...
opencv用于检测带眼镜的眼睛haarcascade_eye_tree_eyeglasses.xml
第一部分 点睛Spring 4.x 第1 章 Spring 基础 2 1.1 Spring 概述 2 1.1.1 Spring 的简史 2 1.1.2 Spring 概述 3 1.2 Spring 项目快速搭建 5 1.2.1 Maven 简介 6 1.2.2 Maven 安装 6 1.2.3 Maven 的pom.xml 7 1.2.4 ...
第一部分 点睛Spring 4.x 第1 章 Spring 基础 2 1.1 Spring 概述 2 1.1.1 Spring 的简史 2 1.1.2 Spring 概述 3 1.2 Spring 项目快速搭建 5 1.2.1 Maven 简介 6 1.2.2 Maven 安装 6 1.2.3 Maven 的pom.xml 7 1.2.4 ...
第一部分 点睛Spring 4.x 第1 章 Spring 基础 2 1.1 Spring 概述 2 1.1.1 Spring 的简史 2 1.1.2 Spring 概述 3 1.2 Spring 项目快速搭建 5 1.2.1 Maven 简介 6 1.2.2 Maven 安装 6 1.2.3 Maven 的pom.xml 7 1.2.4 ...
opencv用于检测眼睛。haarcascade_eye.xml文件
上网保护眼睛--网页底色修改.2011-11-25最新整理,2011-11-25最新整理...
点睛-文本各种编码方式转变.7z
软件名:点睛文本编码查询 功能:本软件用于将文本的字符串转换为 Text、ASCII、Default、Unicode、BigEndianUnicode、UTF-8、UTF-7、GBK、BIG5 之间的互相转换,用于文本查询。转换结果使用十六进制表示。
综合实践-保护眼睛-预防近视-四年级.doc