Thus, when you need to use non-contextual instances, you should first answer to 2 questions

Does the class of the instance has already all annotations (@Inject, qualifiers, Interceptor bindings) at class level?

Can I Let the CDI container create the instance for me?

Your answers will make non-contextual instances more or less easy to use:

Unmanaged.UnmanagedInstance is an important handler that gives you access to all CDI services for the instance you want to obtain, but it should also be kept to perform the dispose() task, which release all dependent beans instances that were created with your instance. Without this call you may face memory leaks in your application

In this case, the class shouldn’t be unproxyable as detailed in the spec .

Keep in mind that letting the container producing the instance ( produce() method) activate optional interceptors and decorators on the instance.

Since CDI 1.1, the API provide the Unmanaged helper class that hides all the work done with InjectionTargetFactory produce a non-contextual instance:

Use case 2: Non-contextual instance whose class doesn’t have the required annotation

Unfortunately, Unmanaged doesn’t provide access to the underlying AnnotatedType for the instance class.

So if you need to add annotations to the metadata model because they are missing on the original class, you’ll have to use the InjectionTargetFactory provided by the container. Note that Unmanaged does the same under the hood.

To request an InjectionTargetFactory from the container, you’ll need first to access the BeanManager .

If you are in CDI programming model (i.e in a CDI bean) simply inject the BeanManager to access it

@Inject BeanManager bm;

If are not CDI programming model, the easiest way to access the BeanManager is ot use the CDI class available since CDI 1.1 (note that it also works in CDI programming model even if direct injection is still preferred to static call done with CDI.current() ).

BeanManager bm = CDI.current().getBeanManager();

In certain circumstance, you may want to retrieve the BeanManager from a JNDI lookup through the java:comp/BeanManager , JNDI name.

The following example show how to create a non-contextual instance from MyClass in which you need to create an injection point (add @Inject ) on the field MyField

public void doSomethingWithContextualMyClass() { BeanManager bm = CDI.current().getBeanManager(); (1) InjectionTargetFactory<MyClass> itf = bm .getInjectionTargetFactory(bm.createAnnotatedType(MyClass.class)); (2) itf.configure() (3) .filterFields(f -> "MyField".equals(f.getJavaMember().getName())) .findFirst() .ifPresent(f -> f.add(InjectLiteral.INSTANCE)); (4) InjectionTarget<MyClass> it = itf.createInjectionTarget(null); (5) CreationalContext<MyClass> cctx = bm.createCreationalContext(null); (6) MyClass myInstance = it.produce(cctx); (7) it.postConstruct(myInstance); (7) it.inject(myInstance,cctx); (7) //Do what you need with myInstance it.preDestroy(myInstance); (8) cctx.release(); (9) }

1 retrieving the BeanManager 2 requesting an InjectionTargetFactory from the BeanManager 3 using the new AnnotatedTypeConfigurator SPI in CDI 2.0 to configure the underlying AnnotatedType . Before CDI 2.0 you’d have to implement AnnotatedType to add your annotation and use it in previous step (2) 4 looking for the MyField field and adding @Inject to it (we use the new InjectLiteral introduced in CDI 2.0) 5 creating the InjectionTarget . As it’s for a non-contextual instance, we create it by passing null (no bean) to the method 6 creating the CreationalContext . As it’s for a non-contextual instance, we create it by passing null (no bean) to the method 7 creating the instance, performing @PostConstruct lifecycle call back and injection 8 call the @Predestroy lifecycle callback 9 release the CreationalContext and all the dependents bean instances