JAVA EE代理模式
我一直在到处读到,当您要求将依赖项注入到 bean 中时,您将被注入该资源实例的代理。我相信我知道代理是什么,它是一个知道如何将消息转发到另一个实例的实例。它还指出,正是这种模式允许容器向该托管 bean 提供服务。
我不太明白这一点。为什么需要代理?这是如何实现的?每个bean都有一个代理对象吗?或者我是否有许多代理转发到一个实例?或者也许两者都不是?
另外,从 GoF 的设计模式一书中,我了解到您必须提供一个充当占位符的代理类。但我从来没有在Java EE中这样做,应用程序服务器是否在运行时创建代理类?
I keep reading everywhere that when you ask for dependencies to be injected in a bean, you are injected a proxy to an instance of that resource. I believe I know what a proxy is, its an instance that knows how to forward messages to another instance. It's also stated that it is this pattern that allows the container to provide services to this managed beans.
I don't understand this quite well. Why is a proxy necessary? And how is this implemented? is there a proxy object for each bean? or do i have many proxies forwarding to one instance? or maybe neither?
Also, from the book design patterns from the GoF, I've read that you have to provide a proxy class that acts as a placeholder. But I never do that in Java EE, does the application server create proxy class during runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代理模式背后的基本假设是您不必从“用户”的角度关心它。代理伪装成声明的类型,并且行为应该相同。附加值来自代理在将调用转发或返回到目标实例之前或之后所做的操作。这就是在容器中实现事务和安全性等的方式。
至于添加的问题:是的,应用程序服务器在必要时创建那些代理类。
The essential assumption behind the proxy pattern is that you should not have to care about it from a "user" point of view. The proxy masquerades as the declared type and should behave the same way. The added value comes from what the proxy does before or after it forwards or returns calls to the target instance. This is how e.g. transactions and security are implemented in the container.
As for the added question: Yes, the application server creates those proxy classes when necessary.