我正在开发一个 Java EE 应用程序,主要是带有 JSF 管理控制台的 JAX-RS,它使用 CDI/Weld 进行依赖项注入 javax.enterprise.context.ApplicationScoped
对象。撇开小调试问题不谈,CDI 在这个项目中表现得非常出色。
现在我需要对 CDI 注入的对象生命周期进行一些非常粗粒度的控制。我需要能够:
我完全意识到,这即使不是违背的话,也与 CDI 和依赖注入的本质相悖。我只是想知道
- 这远程可能吗?
- 如果是,完成工作最简单/最简单/最快/最简单的方法是什么?
I'm working on a Java EE application, primarily JAX-RS with a JSF admin console, that uses CDI/Weld for dependency injection with javax.enterprise.context.ApplicationScoped
objects. Minor debugging issues aside, CDI has worked beautifully for this project.
Now I need some very coarse-grained control over CDI-injected object lifecycles. I need the ability to:
- Remove an injected object from the application context, or
- Destroy/delete/clear/reset/remove the entire application context, or
- Define my own
@ScopeType
and implementing Context
in which I could provide methods to perform one of the two above tasks.
I'm fully aware that this is across, if not against, the grain of CDI and dependency injection in general. I just want to know
- Is this remotely possible?
- If yes, what is the easiest/simplest/quickest/foolproofiest way to get the job done?
发布评论
评论(4)
焊接参考文档 第 2.1.2 节
自定义作用域示例 移植 veiwscoped jsf 注释到 cdi
如果您确实不想采用自定义范围类型的路径..您可以使用不可移植方法通过使用BeanManager .getContext 方法并将此上下文投射到焊接 AbstractSharedContext 可以访问 beanstore 或上下文的 cleanUp() 方法。
查看此主题,了解如何获取适合您环境的 BeanManager 实例
Weld Reference Documentation Section 2.1.2
Custom scope example Porting the veiwscoped jsf annonation to cdi
If you really don't want to take the path of the Custom scope type.. You can use a non-portable method by using BeanManager.getContext method and cast this context in a weld AbstractSharedContext to have access to the beanstore or the cleanUp() method of the context.
Check this thread on how to get a BeanManager instance for your environment
可以在 https://github.com/openknowledge/openknowledge-cdi-extensions/tree/master/openknowledge-cdi-scope/src/main/java/de/openknowledge/cdi/scope也许你必须稍微调整一下实施。
A custom scope which might fit your needs is available at https://github.com/openknowledge/openknowledge-cdi-extensions/tree/master/openknowledge-cdi-scope/src/main/java/de/openknowledge/cdi/scope Maybe you have to adjust the implementation a bit.
开箱即用的只有“对话”范围,可以让您完全控制其生命周期。但如果对话不适合您的需求,您可以创建自己的范围。
创建范围是一项艰巨的工作,但您可以焊接代码并查看对话是如何实现的。
Out of the box there is only the Conversation scope that gives you total control on its lifecycle. But you can create your own scope if conversation doesn't suit your needs.
Creating a scope is a tough job, but you can go to weld code and look how conversation was implemented.
在 CDI 1.1 中,有一个 javax.enterprise.context.spi.AlterableContext 接口,它允许您单独销毁一个 bean 实例。所有正常范围(请求、对话、会话)都是可更改的。
这里的
beanManager
是一个javax.enterprise.inject.spi.BeanManager
实例。 获取它您可以通过 JNDI 查找:或通过 CDI 静态方法:
,但请注意某些 Weld 版本中静态方法的问题:
In CDI 1.1 there is a
javax.enterprise.context.spi.AlterableContext
interface, which allows you to individually destroy a bean instance. All normal scopes (request, conversation, session) are alterable.The
beanManager
here is ajavax.enterprise.inject.spi.BeanManager
instance. You can get it via JNDI lookup:or via CDI static method:
, but be aware of the issues with the static method in some Weld versions: