@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