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

How to expose Jooby module as Avaje injected bean #3415

Closed ludmiloff closed 3 months ago

ludmiloff commented 4 months ago

Hello, I'm looking for a more complex Avaje usage, such as building @Singleton repositories, services etc. which are supposed to access a database or some other Jooby module.

Jooby itself brings its own kind-of service / dependency registration. The GuiceModule integrates with Jooby very well. Unfortunately, this is not the case with Avaje, which is completely not aware of Jooby internals.

In particular, I've managed a workaround (see source below) by declaring the App class being a @Factory. It works for a simple use case, despite it seems not optimal to me.

@Factory
public class App extends Jooby {

  private BeanScope beanScope;

  public App(BeanScope beanScope) {
    super();
    this.beanScope = beanScope;

    install(new HikariModule());
    install(new JdbiModule());
    install(new NettyServer());

    mvc(this.beanScope.get(MyController.class));
  }

  @Bean
  Jdbi getJdbi() {
    return require(Jdbi.class);
  }

  @Bean
  Config appConfig() {
    return getConfig();
  }
}
public class Main {
    public static void main(final String[] args) {
        BeanScope beanScope = BeanScope.builder().build();
        runApp(args, () -> new App(beanScope));
    }
}

Where I stuck is QuartzModule with some jobs being declared as @Singleton. I'm really looking for some more idiomatic way, please advice.

SentryMan commented 4 months ago

I'd do it more like this:

@Factory
public class App extends Jooby {

  public App(MyController controller) {
    install(new HikariModule());
    install(new JdbiModule());
    install(new NettyServer());

    mvc(controller);
  }

  @Bean
  Jdbi getJdbi() {
    return require(Jdbi.class);
  }

  @Bean
  Config appConfig() {
    return getConfig();
  }
}

but yeah I can work on adding a sort of Avaje Inject Module.