Spring中的@Scope註解:web
源碼:spring
@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public [@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 <aop:scoped-proxy/> support in Spring XML. */ ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT; }
屬性:session
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初始化了, 故bean一、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