【Spring】 (2)singleton 单例 ,每次调用只创建一次
来源:互联网 发布:淘宝商城家具城松木床 编辑:程序博客网 时间:2024/05/24 04:22
package com.example.demo_2_1;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Service;/** * Created by WangBin on 2017/4/10. * */@Service@Scope("prototype")//每次调用新建一个bean示例public class DemoPrototypeService {}
package com.example.demo_2_1;import org.springframework.stereotype.Service;/** * Created by WangBin on 2017/4/7. * * 单例 */@Service//默认为 Scope("singleton") 一个容器只有一个 bean实例public class DemoSingletonService {}
package com.example.demo_2_1;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * Created by WangBin on 2017/4/10. * */public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScopeConfig.class); DemoSingletonService service1= context.getBean(DemoSingletonService.class); DemoSingletonService service2= context.getBean(DemoSingletonService.class); System.err.println(service1==service2);//返回 true 证明 该对象 在堆内存只存在一个 DemoPrototypeService serviceA1 = context.getBean(DemoPrototypeService.class); DemoPrototypeService serviceA2 = context.getBean(DemoPrototypeService.class); System.err.println(serviceA1==serviceA2);//返回 false 因为 在 bean上 注解了 Prototype 所以每次调用对象都新建一个bean实例 System.err.println(service1.equals(service2));//equals 也可以用于对象比较 堆内存地址 context.close(); }}
package com.example.demo_2_1;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;/** * Created by WangBin on 2017/4/10. */@Configuration@ComponentScan("com.example.demo_2_1")public class ScopeConfig {}
阅读全文
0 0
- 【Spring】 (2)singleton 单例 ,每次调用只创建一次
- php单例模式实现对象只被创建一次
- php单例模式实现对象只被创建一次
- Spring学习笔记 关于Spring创建Bean的模式-Singleton(单例模式)和Prototype
- Spring学习笔记 关于Spring创建Bean的模式-Singleton(单例模式)和Prototype
- 单例模式(Singleton)的创建
- Singleton---单例模式(创建型)
- 创建型:单例模式(singleton)
- 单例模式(Singleton)的创建
- 关于Spring创建Bean的模式-Singleton(单例模式)和Prototype
- spring 与设计模式(创建型)之单例Singleton
- Spring创建对象小案例,单例singleton,多例prototype(7条)
- 创建型-单例singleton
- php单例模式实现对象只被创建一次 mysql单例操作类
- Singleton(单例)
- 单 例(Singleton)
- Python-单例模式-只初始化一次
- 单例和只初始化一次
- 3daima
- MyView + rc 滑到最后一条 + 条目监听
- MongoDB:13-MongoDB- 覆盖索引查询(Covered Queries)
- JAVA SE回顾--集合(2)
- angular-年龄查询
- 【Spring】 (2)singleton 单例 ,每次调用只创建一次
- PullToRefreshListView的使用
- 多条目加载
- linux下文件传输及归档与压缩
- 属性动画的几种简单用途
- angular-全部删除
- Android中常见的内存泄漏之上下文对象
- Mysql 索引
- Swift小练习(包括答案)