JSR-330
JSR-330 Integration
New in Guice 3.0
JSR-330 standardizes annotations like @Inject and the Provider interfaces for Java platforms. It doesn't currently specify how applications are configured, so it has no analog to Guice's modules.
Guice implements a complete JSR-330 injector. This table summarizes the JSR-330 types and their Guice equivalents.
JSR-330
javax.inject or jakarta.inject Guice
com.google.inject
@Inject @Inject Interchangeable with constraints (See Note 1)
@Named @Named Interchangeable.
@Qualifier @BindingAnnotation Interchangeable.
@Scope @ScopeAnnotation Interchangeable.
@Singleton @Singleton Interchangeable.
Provider Provider Guice's Provider extends JSR-330's (See Note 2).
Note 1: JSR-330 places additional constraints on injection points. Fields must be non-final. Optional injection is not supported. Methods must be non-abstract and not have type parameters of their own. Additionally, method overriding differs in a key way: If a class being injected overrides a method where the superclass' method was annotated with
javax.inject.Injectorjakarta.inject.Inject, but the subclass method is not annotated, then the method will not be injected.
Note 2: Guice's
Providerextends JSR-330's Provider. UseProviders.guicify()to convert a JSR-330Providerinto a GuiceProvider.
Note 3:
- Prior to Guice 6.0, Guice only supported
javax.inject.- Guice 6.0 supports both
javax.injectandjakarta.inject.- Guice 7.0+ only supports
jakarta.inject.
Best Practices
Prefer JSR-330's annotations and Provider interface.
评论
登录后参与评论
KnowForge