16

When I use something like the following in my Quarkus application:

@Path("v1")
@Produces(APPLICATION_JSON)
public class HelloWorldResource {

   @Inject 
   private SomeBean someBean;
}

then I get a warning the following during the build process.

[INFO] [io.quarkus.arc.processor.BeanProcessor] Found unrecommended usage of private members (use package-private instead) in application beans:
    - @Inject field acme.jaxrs.v1.HelloWorldResource#someBean

Everything seems to work just fine so why is Quarkus suggesting that change private to package-private?

geoand
  • 60,071
  • 24
  • 172
  • 190

1 Answers1

27

If a property is package-private, Quarkus can inject it directly without requiring any reflection to come into play.

That is why Quarkus recommends package-private members for injection as it tries to avoid reflection as much as possible (the reason for this being that less reflection means better performance which is something Quarkus strives to achieve).

See section 2 of this guide for more details.

geoand
  • 60,071
  • 24
  • 172
  • 190
  • 2
    What do you suggest we do for Kotlin classes where package private is (still) not a supported visibility modifier? Example `class ReactiveGreetingResource(@Inject var service: ReactiveGreetingService)`. Maybe even a bug in the `BeanProcessor` as it complains about "private members" when in fact it's public? – Marcel Stör Jul 15 '21 at 15:22
  • Yes, that is a false warning. Please open an issue – geoand Jul 16 '21 at 06:15
  • I meanwhile found https://github.com/quarkusio/quarkus/issues/10640 in which you addressed this. – Marcel Stör Jul 16 '21 at 07:04
  • To make the property *private-package*, simply delete the "private" modifier. – GeraldScott Oct 05 '21 at 01:55
  • 1
    @GeraldScott In Kotlin, having no modifier makes a property public. There is no package-private in Kotlin. – Avi Cherry Nov 01 '21 at 19:50
  • Watch out for Client Proxies. With @ConfigProperty, you should use private/getter/setter so that methods can be intercepted to add in the config values. – Wall0p Jan 08 '23 at 14:03