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

Fix avaje module #3426

Merged
merged 5 commits into from
May 19, 2024
Merged

Fix avaje module #3426

merged 5 commits into from
May 19, 2024

Conversation

kliushnichenko
Copy link
Contributor

@jknack
AvajeInjectModule seems doesn't work as expected, at least for me.
With a very basic example (reproduced in test c9c81fd) Jooby inner services injection fails with class cast exception, for example JsonMapper:

Caused by: java.lang.ClassCastException: class io.jooby.internal.ServiceRegistryImpl$$Lambda$458/0x0000000800dbf350 cannot be cast to class com.fasterxml.jackson.databind.json.JsonMapper (io.jooby.internal.ServiceRegistryImpl$$Lambda$458/0x0000000800dbf350 and com.fasterxml.jackson.databind.json.JsonMapper are in unnamed module of loader 'app')

I assume this is due to the fact that services already wrapped into lambda in singletone()

private static <T> Provider<T> singleton(T service) {

Hence, the default binding need to be changed to
beanScope.provideDefault(key.getType(), e.getValue()::get);

Also, Config binding was removed as it is already in service registry

@@ -68,7 +66,7 @@ public void install(Jooby application) throws Exception {
e -> {
final var key = e.getKey();
if (key.getName() == null) {
beanScope.provideDefault(key.getType(), e::getValue);
beanScope.provideDefault(key.getType(), e.getValue()::get);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks


@Singleton
@Path("")
@InjectModule(requires = {JsonMapper.class, Config.class, String.class})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never use Avaje Inject. Why do we need this line?

Copy link
Contributor Author

@kliushnichenko kliushnichenko May 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Avaje Inject, the dependency graph is typically validated when the application compiles. As beans provided by    Jooby Modules are registered at runtime, you must add @InjectModules(requires={String.class, <other Jooby    Module Provided Bean ClassNames>…​}) to inform the avaje processor that these beans are provided at runtime.   

from https://jooby.io/#dependency-injection-avaje-inject-mvc-routes :)

@jknack jknack added the bug label May 19, 2024
@jknack jknack added this to the 3.1.1 milestone May 19, 2024
@jknack jknack merged commit 85647ea into jooby-project:3.x May 19, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants