Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should Instance<T> and Provider<T> be used to inject beans in CDI?

I've been reading the API documentation of Instance<T> and Provider<T>, but it isn't completely clear when they should be used.

What's the difference between the following approaches?

@Inject
MyBean bean;
@Inject
Instance<MyBean> bean;
@Inject
Provider<MyBean> bean;
like image 279
cassiomolin Avatar asked Oct 14 '15 14:10

cassiomolin


1 Answers

Provider<T> is a JSR-330 interface which is extended by the CDI interface Instance<T>.

Injecting MyBean, your application will throw an exception during startup when there is no matching bean or more than one matching bean.

Injecting Instance<MyBean>, bean resolution is delegated to the application: you can iterate over all candidate beans and select() the one you want or call isUnsatisfied() and decide what to do when there is no matching bean.

For beans with @Dependent scope, calling Instance.get() will create a new instance for each invocation, and you should invoke Instance.destroy(t) for each such instance when you no longer need it.

Provider just has the get() method, but no destroy() or select() and does not support iteration. In a CDI environment, for any use case addressed by Provider<T>, you had better use Instance<T> instead.

like image 85
Harald Wellmann Avatar answered Nov 04 '22 14:11

Harald Wellmann