java 定义 Spring 请求范围 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5895038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
defining Spring request scope bean
提问by Spring
For using spring request scopebean is this definition correct?
对于使用 spring请求范围bean 这个定义是否正确?
<bean id="shoppingCart" class="ShoppingCart" scope="request">
<!-- This requires CGLIB -->
<aop:scoped-proxy/>
</bean>
I modified this from a session scopebean example, and changed only the scope definition, not sure about the proxy thing
我从会话作用域bean 示例中修改了它,只更改了作用域定义,不确定代理的事情
I took this example from this link, you can see the full xml:
我从这个链接中获取了这个例子,你可以看到完整的 xml:
http://wheelersoftware.com/articles/spring-session-scoped-beans-2.html
http://wheelersoftware.com/articles/spring-session-scoped-beans-2.html
回答by Roadrunner
Generally - yes, it's correct.
一般来说 - 是的,这是正确的。
If for every request You'd retrieve the request scoped bean directly from the BeanFactory
, then You don't need the proxy.
如果对于每个请求,您都直接从 检索请求范围的 bean BeanFactory
,那么您不需要代理。
But You need the proxy if You're going to use the request soped bean as a depenedncy to singleton scoped bean, for example like this:
但是,如果您要使用请求 soped bean 作为对单例作用域 bean 的依赖,则需要代理,例如:
@Controller
public class MyController {
@Autowired
private ShoppingCart shoppingCart;
}
See this referencepage for more details about scoped beans.
有关作用域 bean 的更多详细信息,请参阅此参考页。
As a side note I'd advise to use standard JDK interface-based poxies instead of CGLIB whenever possible. More about proxying with spring can be found in documentation.
作为旁注,我建议尽可能使用标准的基于 JDK 接口的 poxies 而不是 CGLIB。更多关于使用 spring 代理的信息可以在文档中找到。