如何绑定在Guice中使用注释值的provider? [英] How to bind with provider which uses annotation value in Guice?

查看:173
本文介绍了如何绑定在Guice中使用注释值的provider?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如:



有没有办法绑定提供者在Google Guice中解释目标的注释值? pre> bind(Resource.class)
.annotatedWith(MyAnnotation.class)
.toProvider(new MyProvider< MyAnnotation,Resource> {
public Resource get (MyAnnotation anno){
return resolveResourceByAnnoValue(anno.value());
}
});

我想通过注释绑定初始化Android Activity类的字段。
它必须通过唯一的ID来占用多个资源。



原始方式:

  public class TestActivity extends Activity {
private TextView textView;
private Button testButton;

public void onAfterCreate(...){
//设置UI声明资源。
setContentView(R.layout.activity_test);

//初始化字段,必须在设置ui定义后才能完成。
textView =(TextView)findViewById(R.id.textView);

....初始化其他字段,挂钩...

...
}

我想以声明的方式绑定UI和它的字段,而不是实际上喜欢上述:

  @ResourceID(R.layout.activity_test)
public class TestActivity extends InjectiveActivity {
@ResourceID(R.id.textView)//自动生成的静态资源id常量
私有TextView textView;

@ResourceID(R.id.testButton)
private Button testButton;

...
}


解决方案

这是不可能的。



如果 @MyAnnotation 是一个绑定注释,它将使用其等于方法进行比较。 @MyAnnotation(5)资源将被绑定到 @MyAnnotation(5)资源,并且根本不匹配与 @MyAnnotation(6)资源相比。查看此SO答案了解更多信息。如在这个答案中,你可以循环浏览你可能的注释值,并且如果你感觉到,你可以单独绑定每一个。



如果 @MyAnnotation 不是绑定注释,您将无法从您的提供商访问它。如此SO答案所述,则拒绝的功能将注入站点信息添加到提供程序或依赖关系本身。



最好的办法是创建一个 @Assisted 注入(或手动工厂)接受参数:

  class MyConsumer {
final Resource resource;
@Inject MyConsumer(Resource.Factory resourceFactory){
int以前的AnnotatedValue = 5;
this.resource = resourceFactory.createWithValue(以前的NotnvalatedValue);
}
}

您还可以考虑使用自定义注入,这将允许您使用除 @Inject ,可以使用运行时注释值。


Is there any way to bind with provider which interprets target's annotation value in Google Guice?

Example:

bind(Resource.class)
    .annotatedWith(MyAnnotation.class)
    .toProvider(new MyProvider<MyAnnotation, Resource>{
        public Resource get(MyAnnotation anno){
            return resolveResourceByAnnoValue(anno.value());
        }
    });

I want to initialize field of an Android Activity class by annotated binding. It should have to take multiple resources by it's unique Id.

Original Way:

public class TestActivity extends Activity{
    private TextView textView;
    private Button testButton;

    public void onAfterCreate(...){
        // set UI declaration resource.
        setContentView(R.layout.activity_test);

        // initialize fields, it must be done after setting ui definition.
        textView = (TextView) findViewById(R.id.textView);

        .... initialize other fields, hook them...

    ...
}

I want to bind UI and it's field in declarative way, not pragmatically likes above:

@ResourceID(R.layout.activity_test)
public class TestActivity extends InjectiveActivity{
    @ResourceID(R.id.textView) // Auto generated static resource id constant
    private TextView textView;

    @ResourceID(R.id.testButton)
    private Button testButton;

    ...
}

解决方案

This isn't possible as such.

If @MyAnnotation is a binding annotation, it will be compared using its equals method. @MyAnnotation(5) Resource will be bound to @MyAnnotation(5) Resource, and that will not match at all compared to @MyAnnotation(6) Resource. Check out this SO answer for more. As in that answer, you could loop through your possible annotation values and bind each one individually, if you feel like it.

If @MyAnnotation isn't a binding annotation, you won't be able to access it at all from your provider. As mentioned in this SO answer, it is a rejected feature to add injection-site information to the provider or dependency itself.

Your best bet is to create an @Assisted injection (or manual factory) to accept the parameter:

class MyConsumer {
  final Resource resource;
  @Inject MyConsumer(Resource.Factory resourceFactory) {
    int previouslyAnnotatedValue = 5;
    this.resource = resourceFactory.createWithValue(previouslyAnnotatedValue);
  }
}

You may also consider using Custom Injections, which will let you use an arbitrary annotation other than @Inject, which may use runtime annotation values however you'd like.

这篇关于如何绑定在Guice中使用注释值的provider?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆