Quarkus: CDI.current().select(Foo.class).get() does not work if Foo is not injected

Created on 23 May 2019  路  12Comments  路  Source: quarkusio/quarkus

CDI.current().select(...).get() is useful to get a bean whose type is known dynamically, with quarkus it only works if the type is injected somewhere.

For example :
Foo foo = CDI.current().select(Foo.class).get() causes a javax.enterprise.inject.UnsatisfiedResolutionException

Problem solved with

@Inject Foo foo;

even if foo is not used.

If it is due to build time optimizations, how about an option to flag the classes (here Foo) to register theses beans even if not injected anywhere ? Something like a retention policy.

arearc kinenhancement

All 12 comments

@mkouba can confirm but I'd say it's because the class is being removed because ArC can tell it isn't needed

I would like to fix this

A while back I had proposed something in between removing all unused and not removing any unused beans at all. The idea was to keep all beans defined in the application - see this.

Perhaps it's something we should reevaluate? If it is something that we think makes sense, then I can redo the PR.

Another idea might be to add some kind of nnotation that will ensure that Arc never removes the bean?

@kenfinnigan I confirm.

@Dufgui This optimization can be disabled using quarkus.arc.remove-unused-beans=false. See https://quarkus.io/guides/cdi-reference#remove_unused_beans.

@geoand I like both of your ideas. Actually, I've been thinking of the annotation for a long time. It could be just @Unremovable. WRT keep app beans - it could be a special "mode", so that we could reuse the quarkus.arc.remove-unused-beans property. So we could have the following modes: all/true, framework (keep app beans), none/false.

@mkouba @Unremovable sounds perfect!

I can implement both of these two proposals in the following days (probably won't have any spare time today) if you agree.

@geoand of course! ;-)

thanks for your reply. Yes i see the remove unused flag (in the code, not in the doc ;o) )
I just try the approach to keep the optimization. But as I really like the @Unremovable idea.

@mkouba Now for the second part of proposed solution, how would you like to handle the quarkus.arc.remove-unused-beans value? Currently that value is a boolean but we would like to make it an Enum I guess. That would mean breaking existing applications that define this configuration.
What do you propose we do?

Could we just use a string and map it to the following modes?

Mode | Values
------------ | -------------
all | all, true
framework (keep app beans) | fwk
none | none, false

That sounds interesting. It wouldn't break existing applications (unless I am missing something) and we could support all the required modes.

Sounds good! I'll take it up soon :wink:

@mkouba should we close this now with that we have a couple related solutions in place?

@geoand Yes ;-).

Was this page helpful?
0 / 5 - 0 ratings