jooby-project / jooby

The modular web framework for Java and Kotlin
https://jooby.io
Apache License 2.0
1.7k stars 200 forks source link

Add Avaje Inject Module #3416

Closed SentryMan closed 3 months ago

SentryMan commented 4 months ago

First pass at an avaje inject module

helps alleviate #3415

SentryMan commented 4 months ago

@ludmiloff how's this?

ludmiloff commented 4 months ago

@ludmiloff how's this?

@SentryMan, this does not work, at least for me. This is the error:

[2024-04-25 17:01:53,520]-[main] ERROR io.jooby.Jooby - Application initialization resulted in exceptionjava.lang.IllegalStateException: Expecting only 1 bean match but have multiple secondary beans org.jdbi.v3.core.Jdbi@7754195b and org.jdbi.v3.core.Jdbi@7754195b
    at jars//io.avaje.inject.spi.DContextEntry$EntryMatcher.checkSecondary(DContextEntry.java:195)

[ERROR] execution of com.Main resulted in exception
io.jooby.exception.StartupException: Application initialization resulted in exception
    at io.jooby.Jooby.createApp (Jooby.java:1222)
    at io.jooby.Jooby.runApp (Jooby.java:1183)
    at io.jooby.Jooby.runApp (Jooby.java:1143)
    at com.Main.main (Main.java:10)
Caused by: java.lang.IllegalStateException: Expecting only 1 bean match but have multiple secondary beans org.jdbi.v3.core.Jdbi@7754195b and org.jdbi.v3.core.Jdbi@7754195b
    at io.avaje.inject.spi.DContextEntry$EntryMatcher.checkSecondary (DContextEntry.java:195)
    at io.avaje.inject.spi.DContextEntry$EntryMatcher.candidate (DContextEntry.java:189)
    at io.avaje.inject.spi.DContextEntry$EntryMatcher.findMatch (DContextEntry.java:134)
    at io.avaje.inject.spi.DContextEntry$EntryMatcher.match (DContextEntry.java:118)
    at io.avaje.inject.spi.DContextEntry.get (DContextEntry.java:53)
    at io.avaje.inject.spi.DBeanMap.get (DBeanMap.java:115)
    at io.avaje.inject.spi.DBuilder.getMaybe (DBuilder.java:141)
    at io.avaje.inject.spi.DBuilder.getBean (DBuilder.java:384)
    at io.avaje.inject.spi.DBuilder.get (DBuilder.java:333)
    at com.DataCache$DI.build (DataCache$DI.java:20)

where my DataCache class is declared as singleton with constructor like this

    @Inject
    public DataCache(Jdbi jdbi, Config config) {
       ......
    }

My proposal above works fine, though.

Should I prepare some example code for experiments?

SentryMan commented 4 months ago

you mean with the @InjectModule(requires={String.class}) thing? For that, you only need to say String.class once and it'll take care of things. I'll need to make a note of that in the docs.

ludmiloff commented 4 months ago

you mean with the @InjectModule(requires={String.class}) thing? For that, you only need to say String.class once and it'll take care of things. I'll need to make a note of that in the docs.

Yes, this way it works, thanks.

SentryMan commented 4 months ago
  1. Using this module needs a special maven/gradle plugin and setup for Avaje code generation

This should only be required if you are running on the module-path and also happen to have custom avaje autoconfig libraries. But yeah it is what it is