考虑以这种方式定义的两个Spring bean:
@Component
class SingletonScopedBean{@Autowired private PrototypeScopedBean prototypeScopedBean;public String getState(){return this.prototypeScopedBean.getState();}
}@Component
@Scope(value="prototype")
class PrototypeScopedBean{private final String state;public PrototypeScopedBean(){this.state = UUID.randomUUID().toString();}public String getState() {return state;}
}
在这里,将原型作用域bean注入到Singleton作用域bean中。
现在,考虑使用以下bean进行此测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ScopedProxyTest {@Autowired private SingletonScopedBean singletonScopedBean;@Testpublic void testScopedProxy() {assertThat(singletonScopedBean.getState(), not(equalTo(singletonScopedBean.getState())));}@Configuration@ComponentScan("org.bk.samples.scopedproxy")public static class SpringContext{}}
需要注意的是,这里仅创建了1个PrototypeScopedBean实例,并且将1个实例注入到SingletonScopedBean中,因此上述测试实际上期望每次调用getState()方法都会产生一个PrototypeScopedBean新实例失败。 。
如果对PrototypeScopedBean的每个请求都需要一个新实例(并且通常,如果作用域较长的bean的作用域较短,则需要遵守较短的作用域),那么有几种解决方案:
1.查找方法注入–可以在这里阅读
2.更好的解决方案是使用范围代理 -
可以使用@Configuration以这种方式指定作用域代理:
@Component
@Scope(value="prototype", proxyMode=ScopedProxyMode.TARGET_CLASS)
class PrototypeScopedBean{private final String state;public PrototypeScopedBean(){this.state = UUID.randomUUID().toString();}public String getState() {return state;}}
进行此更改后,注入到SingletonScopedBean中的bean并不是PrototypeScopedBean本身,而是该bean的代理(使用CGLIB或Dynamic代理创建),并且该代理了解范围并根据范围的要求返回实例,测试应现在可以正常工作了。
参考: all和其他博客中的JCG合作伙伴 Biju Kunjummen提供的Spring Scoped Proxy 。
翻译自: https://www.javacodegeeks.com/2012/08/spring-scoped-proxy.html