Java Reference
In-Depth Information
//...
}
Accessing the non contexual instance of a bean
This version also added the ability to inject and execute lifecycle callbacks of un-
managed instances of beans. The following code demonstrates how to inject and
execute lifecycle callbacks of non contexual instances of the bean
Student
:
Unmanaged<Student> unmanagedBean =
newUnmanaged<Student>(Student.class);
UnmanagedInstance<Student> beanInstance
=unmanagedBean.newInstance();
Student foo
=beanInstance.produce().inject().postConstruct().get();
// Usage of the injected bean
beanInstance.preDestroy().dispose();
Accessing the current CDI container
The CDI Specification 1.1 has added the ability to access the current CDI container
programmatically and perform some operations. The following code demonstrates
how to access a CDI container to explicitly destroy a context object:
CDI container = CDI.current();
container.destroy(destroableManagedInstance);
Destroying CDI bean instances explicitly
Toallowexplicitdestructionofbeaninstancesinapplications,CDI1.1hasintroduced
the
AlterableContext
interface, which contains the
void destroy
(
Contextu-
al<?> contextual
) method. Extensions should implement this interface instead
of the
Context
interface.