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 implementingContext
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?
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:
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.
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
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.