Spring中的@Scope注解

Spring中的@Scope注解:

源码:
	@Target({ElementType.TYPE, ElementType.METHOD})
	@Retention(RetentionPolicy.RUNTIME)
	@Documented
	public @interface 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 <aop:scoped-proxy/> 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
	
	

		
		
		

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值