User's Guide

JSR-330

qianmoQqianmoQ· 更新于 2026-09-23· 阅读 7 分钟· 0 次阅读

登录后可跨设备保存划线和私人笔记登录

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.Inject or jakarta.inject.Inject, but the subclass method is not annotated, then the method will not be injected.

Note 2: Guice's Provider extends JSR-330's Provider. Use Providers.guicify() to convert a JSR-330 Provider into a Guice Provider.

Note 3:

  • Prior to Guice 6.0, Guice only supported javax.inject.
  • Guice 6.0 supports both javax.inject and jakarta.inject.
  • Guice 7.0+ only supports jakarta.inject.

Best Practices

Prefer JSR-330's annotations and Provider interface.

评论

登录后参与评论

正在加载评论…