我想提供一个Guice AbstractModule,它有一些带有受保护注释的提供者,因此这些值只能由扩展类使用。
public abstract class AssetModule extends AbstractModule {
/** Protected annotation to inject the underlying {@link Asset} payload. */
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
protected @interface ProtectedAssetPayload {}
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface ParentPublicAnnotation {}
@Override
protected void configure() {
install(new Framework());
}
private final class Framework extends AbstractModule {
@Override
protected void configure() {}
@Provides
@ParentPublicAnnotation
String getPublicValue() {
return "public value";
}
@Provides
@ProtectedAssetPayload
protected String getAssetPayload() {
return "payload";
}
}
}
这将允许我扩展资产:
public final class SomeAssetModule extends AssetModule {
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface ChildPublicAnnotation {}
@Provides
@ChildPublicAnnotation
String concactToPayload(@ProtectedAssetPayload String payload) {
return String.format("concact_%s", payload);
}
}
并在第三类中使用两个公开注释的绑定:
public final class Foo {
private final String a;
private final String b;
@Inject
public Foo(@ParentPublicAnnotation String a, @ChildPublicAnnotation String b) {
this.a = a;
this.b = b;
}
}
当我尝试运行时,我得到的错误如下:
No implementation for java.lang.String annotated with @ParentPublicAnnotation() was bound
No implementation for java.lang.String annotated with @ProtectedAssetPayload() was bound
Guice似乎只在实现SomeAssetModule类中绑定了provider方法,并忽略了超类中的绑定。 关于如何强制Guice实际绑定超类中提供方法的任何想法?
干杯!