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

ENH: Add Plugin API for 'default providers' and BeanScopeBuilder.provideDefault() methods #258

Closed
rbygrave opened this issue Jan 12, 2023 · 3 comments · Fixed by #259
Closed
Assignees
Milestone

Comments

@rbygrave
Copy link
Contributor

This allows plugins to add a 'default provider' (as a @Secondary) that can be used when nothing else provides that dependency.

For example, Jsonb can have a plugin to use () -> Jsonb.builder().build() as a provider like:

/**
 * Plugin for avaje inject that provides a default Jsonb instance.
 */
public final class DefaultJsonbProvider implements io.avaje.inject.spi.Plugin {

  @Override
  public Class<?>[] provides() {
    return new Class<?>[]{Jsonb.class};
  }

  @Override
  public void apply(BeanScopeBuilder builder) {
    builder.provideDefault(Jsonb.class, () -> Jsonb.builder().build());
  }
}
@rbygrave rbygrave self-assigned this Jan 12, 2023
@rbygrave rbygrave added this to the 8.12 milestone Jan 12, 2023
rbygrave added a commit that referenced this issue Jan 12, 2023
@SentryMan
Copy link
Collaborator

So if a dependency provides a plugin, I don't need to define @InjectModules explicitly? Or did I misread this entirely?

@rbygrave
Copy link
Contributor Author

Yes, you got it.

Jsonb and the Validator are 2 good examples. For something to be a plugin:

  1. It only provides one component
  2. The component it provides must not have any dependency
  3. The component it provides is not named/qualified and has @Secondary priority so will be used only when nothing else provides the same component

So we'd use a Plugin rather than a full blown avaje-inject module with @InjectModules etc because with the plugin it ends up as approximately 2 lines of code and a full blown avaje-inject module instead looks a bit more like serious overkill.

@SentryMan
Copy link
Collaborator

Yeah I figured. I'm getting a dependency not provided error on my javalin repo when I use the new validator. I guess the service loader can't load the plugins at compile time. #263 fixes this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants