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
Spring中的@Scope注解
最新推荐文章于 2025-01-14 22:22:29 发布