CDI - 编写自定义上下文和范围
我想为一些基于 CDI 的项目提供自己的背景。我需要(想要)自定义范围,以便我可以隔离组件的生存时间和位置。
要实现您自己的上下文,您需要实现 Context 接口,该接口非常不言自明,但是在创建它时如何或在哪里真正定义它?
I would like to have my own contexts for some CDI-based projects. I need (want) custom scopes so that I can isolate the how long a component lives and where.
To implement your own context, you need to implement the Context interface which is pretty self-explanatory, but how or where to you really define when it is created?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有测试过,但我相信这会起作用。对于应用程序中想要的每个自定义范围/上下文,您只需在初始化容器时通过扩展添加该上下文:
现在,技巧是,您需要保存对该上下文的引用,以便在您想要启动时或者停止它,你可以。大概是这样的:
应该可以了,没有大量的文档,所以我希望这会有所帮助。
有兴趣的可以看看这里的来源:
http://github.com/walterjwhite/server.web.application
沃尔特
I haven't tested this yet, but I believe this will work. For each custom scope/context you want in your application, you simply need to add that context via an extension when the container is initialized:
Now, the trick is, you need to hold a reference to that context so then when you want to start or stop it, you can. That would be something like:
That should do it, there isn't a wealth of documentation out there, so I hope this helps.
Anyone interested, check out the source here:
http://github.com/walterjwhite/server.web.application
Walter