博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring中的@Scope注解
阅读量:7156 次
发布时间:2019-06-29

本文共 4378 字,大约阅读时间需要 14 分钟。

hot3.png

Spring中的@Scope注解:

源码:

@Target({ElementType.TYPE, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic [@interface](https://my.oschina.net/u/996807) Scope {	/**	 * Specifies the scope to use for the annotated component/bean.	 * @see ConfigurableBeanFactory#SCOPE_SINGLETON	 * @see ConfigurableBeanFactory#SCOPE_PROTOTYPE	 * @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST	 * @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION	 */	String value() default ConfigurableBeanFactory.SCOPE_SINGLETON;	/**	 * Specifies whether a component should be configured as a scoped proxy	 * and if so, whether the proxy should be interface-based or subclass-based.	 * Defaults to ScopedProxyMode#NO, indicating that no scoped proxy should be created.	 * Analogous to 
support in Spring XML. */ ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;}

属性:

value	singleton	表示该bean是单例的。(默认)	prototype	表示该bean是多例的,即每次使用该bean时都会新建一个对象。	request		在一次http请求中,一个bean对应一个实例。	session		在一个httpSession中,一个bean对应一个实例。	proxyMode	DEFAULT			不使用代理。(默认)	NO				不使用代理,等价于DEFAULT。	INTERFACES		使用基于接口的代理(jdk dynamic proxy)。	TARGET_CLASS	使用基于类的代理(cglib)。

举例:

场景:	ExcelParseServiceImpl是单例的(singleton)	BaiduExcelParseServiceImpl是多例的(prototype)	ExcelParseServiceImpl依赖于BaiduExcelParseServiceImpl目标:	保证BaiduExcelParseServiceImpl无论在什么时候都是多例的。代码:	单例的bean:			@Component // 注:默认为单例		public class ExcelParseServiceImpl {			@Autowired			private BaiduExcelParseService baiduExcelParseService;			public BaiduExcelParseService getBaiduExcelParseService() {				return baiduExcelParseService;			}			public void setBaiduExcelParseService(BaiduExcelParseService baiduExcelParseService) {				this.baiduExcelParseService = baiduExcelParseService;			}		}			多例的bean:			case1:不使用代理的多例bean			@Service()				@Scope(value="prototype")			public class BaiduExcelParseServiceImpl implements BaiduExcelParseService {				// ...			}				case2:使用代理的多例bean			@Service()			@Scope(value="prototype",proxyMode = ScopedProxyMode.TARGET_CLASS)			public class BaiduExcelParseServiceImpl implements BaiduExcelParseService {				// ...			}		case3:使用代理的单例bean			@Service()			@Scope(value="singleton",proxyMode = ScopedProxyMode.TARGET_CLASS)			public class BaiduExcelParseServiceImpl implements BaiduExcelParseService {				// ...			}测试类:	@RunWith(SpringJUnit4ClassRunner.class)	@ContextConfiguration(locations = "classpath:spring-test.xml")	public class BaiduExcelParseServiceImplTest extends AbstractJUnit4SpringContextTests{				@Autowired		private ExcelParseServiceImpl excelParseService;		@Test		public void test(){			BaiduExcelParseService bean1 = MyApplicationContextUtil.getContext().getBean(ExcelParseServiceImpl.class).getBaiduExcelParseService();			BaiduExcelParseService bean2 = MyApplicationContextUtil.getContext().getBean(ExcelParseServiceImpl.class).getBaiduExcelParseService();			BaiduExcelParseService bean3 = MyApplicationContextUtil.getContext().getBean(BaiduExcelParseService.class);			BaiduExcelParseService bean4 = MyApplicationContextUtil.getContext().getBean(BaiduExcelParseService.class);	//			System.out.println("---" + bean1);			System.out.println("---" + bean2);			System.out.println();			System.out.println("---" + bean3);			System.out.println("---" + bean4);		}			}测试结果:	case1:@Scope(value="prototype") 不使用代理的多例bean		---com.jxn.service.BaiduExcelParseServiceImpl@16029e2f		---com.jxn.service.BaiduExcelParseServiceImpl@16029e2f		---com.jxn.service.BaiduExcelParseServiceImpl@3b2db389		---com.jxn.service.BaiduExcelParseServiceImpl@45f1413c				说明:			ExcelParseServiceImpl是单例的,ExcelParseServiceImpl在实例化的时候已经将自己的成员变量baiduExcelParseService初始化了,			故bean1、bean2其实都是这个被赋过值的baiduExcelParseService。	case2:@Scope(value="prototype",proxyMode = ScopedProxyMode.TARGET_CLASS) 使用代理的多例bean			---com.jxn.service.BaiduExcelParseServiceImpl@16b7e04a		---com.jxn.service.BaiduExcelParseServiceImpl@661db63e				---com.jxn.service.BaiduExcelParseServiceImpl@5cf2f5d6		---com.jxn.service.BaiduExcelParseServiceImpl@429f0ca8			case3:@Scope(value="singleton",proxyMode = ScopedProxyMode.TARGET_CLASS) 使用代理的单例bean		---com.jxn.service.BaiduExcelParseServiceImpl@4dac40b		---com.jxn.service.BaiduExcelParseServiceImpl@4dac40b				---com.jxn.service.BaiduExcelParseServiceImpl@4dac40b		---com.jxn.service.BaiduExcelParseServiceImpl@4dac40b

转载于:https://my.oschina.net/u/1399755/blog/1787172

你可能感兴趣的文章
lombok 安装
查看>>
virtualbox+centos 7 实现宿主机器互通
查看>>
Python爬虫学习笔记——防豆瓣反爬虫
查看>>
安装MySQL最后一步出现错误Error Nr.1045
查看>>
基于注解实现SpringBoot多数据源配置
查看>>
02 面向对象之:类空间问题以及类之间的关系
查看>>
LeetCode 402: Remove K Digits
查看>>
我的Python成长之路---第八天---Python基础(23)---2016年3月5日(晴)
查看>>
【PRINCE2】PRINCE2的特点
查看>>
CCF NOI1074 2的幂次方表示
查看>>
Redis学习笔记(5)-Set
查看>>
性能分析
查看>>
寄存器使用
查看>>
LOJ#6053 简单的函数
查看>>
Python程序输出到文件中
查看>>
C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变(转)
查看>>
sql企业管理器WEB版
查看>>
别再犯低级错误,带你了解更新缓存的四种Desigh Pattern
查看>>
用 Flask 来写个轻博客 (29) — 使用 Flask-Admin 实现后台管理 SQLAlchemy
查看>>
5.2二叉搜索树遍历(前序、中序、后序、层次、广度优先遍历)
查看>>