Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong bean scopes used in project #3

Open
dartartem opened this issue Jan 19, 2021 · 0 comments
Open

Wrong bean scopes used in project #3

dartartem opened this issue Jan 19, 2021 · 0 comments

Comments

@dartartem
Copy link
Contributor

Problem:

instead of @Produces I accidentally used @Singleton https://github.com/eventuate-tram/eventuate-tram-core-quarkus/blob/master/eventuate-tram-quarkus-in-memory/src/main/java/io/eventuate/tram/quarkus/inmemory/TramInMemoryConfiguration.java and tests passed. However, if change it to @Produces (As it is used everywhere) tests failed.

Reason:

@Produces does not mean "create bean with scope defined in class annotation" in case of TramInMemoryConfiguration - @ApplicationScoped.

It means that scope is not defined and defaulted to @Dependent (new instance each time on inject).

Point 12: https://quarkus.io/guides/cdi

So, bean definitions should be annotated with desired scope as configuration classes.

Desired behavior:

Beans should be in global scope for application.

There are 2 options (Point 10: https://quarkus.io/guides/cdi):

@javax.enterprise.context.ApplicationScoped

A single bean instance is used for the application and shared among all injection points. The instance is created lazily, i.e. once a method is invoked upon the client proxy.

@javax.inject.Singleton

Just like @ApplicationScoped except that no client proxy is used. The instance is created when an injection point that resolves to a @Singleton bean is being injected.

Currently, eventuate quarkus projects supposed to use @ApplicationScoped

However, it has a drawback that defined beans should have default constructor: https://stackoverflow.com/questions/48410451/why-do-i-need-a-no-args-constructor-to-use-applicationscoped-beans-with-construc

Not all eventuate bean classes have it.

And there are no used advantages in @ApplicationScoped (Point 11: https://quarkus.io/guides/cdi) to change them:

  • Lazy instantiation - the instance is created once a method is invoked upon the proxy.
  • Ability to inject a bean with "narrower" scope to a bean with "wider" scope; i.e. you can inject a @RequestScoped bean into an @ApplicationScoped bean.
  • Circular dependencies in the dependency graph. Having circular dependencies is often an indication that a redesign should be considered, but sometimes it’s inevitable.
  • In rare cases it’s practical to destroy the beans manually. A direct injected reference would lead to a stale bean instance.

Solution:

Use @Singleton as default scope.

dartartem added a commit to dartartem/eventuate-common-quarkus that referenced this issue Jan 19, 2021
dartartem added a commit to dartartem/eventuate-messaging-kafka-quarkus that referenced this issue Jan 19, 2021
dartartem added a commit to dartartem/eventuate-tram-core-quarkus that referenced this issue Jan 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant