指南:范围相关问题
我想使用依赖注入框架。
在评估过程中,我得出的结论是 Google Guice 似乎最适合我的需求。
然而,我想到了一些问题:
想象一个 Web 应用程序,其中用户可以在 http 会话中拥有独立的窗口。
Session
范围太宽泛,而Request
范围对我来说太窄。
有一个范围可以帮助我吗? (我称之为“窗口
”或“控制器实例
”范围)编写自定义作用域是否存在任何陷阱 ?
我们的 Web 应用程序和几个独立的控制台应用程序使用相同的类。我面临的问题是类的范围取决于仅在运行时已知的应用程序类型。
例如,在独立应用程序中,范围将是“无范围”或“每线程范围”,而在 Web 应用程序中,它将绑定到会话
/“窗口
”。
如何解决这个问题?
I'd like to use a dependency injection framework.
During my evaluation I came to the conclusion that Google Guice seems to fit best for my demands.
However, some questions came into my mind:
Imagine a web application in which a user can have independent windows within a http session. The
Session
scope is too general while theRequest
scope is too narrow for me.
Is there a scope which will help me out? (something I would call "window
" or "controller instance
" scope)Are there any pitfalls writing a custom scope?
Our web application and several stand alone console applications are using the same classes. I am facing the problem that the scope of a class depends on the application type which is only known at runtime.
E.g. in a standalone application the scope would be "No-Scope" or "per-Thread Scope", while in a web application it would be bound to aSession
/"window
".
How to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回答3.为您的版本使用不同的模块,并在那里设置范围。
To answer 3. use different modules for your versions, and set the scopes there.
您可以将作用域与应用程序上下文处理程序一起使用,这有助于确定作用域逻辑的工作方式。然后使用相同的自定义范围,您可以控制如何在运行时创建对象。
You can use scope with application context handlers which help in deciding how your scoping logic works. Then using the same custom scope you can control how the objects get created at runtime.