Dependency injection 使用字段名作为键将值从属性文件注入字段

Dependency injection 使用字段名作为键将值从属性文件注入字段,dependency-injection,annotations,guice,Dependency Injection,Annotations,Guice,在guice中,您可以使用@Named注释将属性文件中的值注入类字段 @Inject @命名(“nameLengthMin”) 私有整数名长度最小; 我想构建一个版本的@Named注释,它不需要键参数,而是使用字段名作为键在属性文件中搜索 //将字段名作为键 @注入 @命名 私有整数名长度最小; 我正试图弄清楚如何使用guice实现这一点,但我对guice和编写自己的注释相当陌生 如何编写注释,将注释字段作为参数提供给我?这应该可以通过使用自定义注入实现,请参阅 如果使用InjectandN

在guice中,您可以使用
@Named
注释将属性文件中的值注入类字段

@Inject
@命名(“nameLengthMin”)
私有整数名长度最小;
我想构建一个版本的
@Named
注释,它不需要键参数,而是使用字段名作为键在属性文件中搜索

//将字段名作为键
@注入
@命名
私有整数名长度最小;
我正试图弄清楚如何使用guice实现这一点,但我对guice和编写自己的注释相当陌生


如何编写注释,将注释字段作为参数提供给我?

这应该可以通过使用自定义注入实现,请参阅

如果使用InjectandNamed,则必须处理guice的内部实现,因此我建议创建一个自定义的“InjectProperty”绑定注释(如InjectLogger)。然后,您可以按照示例进行操作,您的“PropertyMembersInjector”可以读取属性并检查字段名是否为有效键,然后强制转换属性值并在字段上进行设置。
这将是一些编码,但它是可行的

我知道我在挖掘一条老线索,但我想与其他在谷歌上搜索的人分享我的发现

这个功能很久以前就有了,但现在还不受支持。最合适的注释是的注释@jan galinski没有错,但是有第三方解决方案已经为您完成了这项工作

添加了此功能,但似乎已被放弃(而且并非所有功能都与Guice 3配合使用)。尽管如此,这里有一个例子说明了如何做到这一点

@GrabResolver(name='GuiceyFruit', root='http://guiceyfruit.googlecode.com/svn/repo/releases')
@Grab(group='org.guiceyfruit', module='guiceyfruit-spring', version='2.0')
@Grab(group='com.google.inject', module='guice', version='3.0')
import javax.annotation.Resource;
import com.google.inject.name.Names;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import org.guiceyfruit.spring.SpringModule;

public class Main {
    public static void main(String[] args) {
        Box box = new Box();
        System.out.println("thingOne before injection='"+box.thingOne+"'");
        System.out.println("thingTwo before injection='"+box.thingTwo+"'");
        Guice.createInjector(new SpringModule(), new AbstractModule() {
            @Override
            protected void configure() {
                bind(String.class).annotatedWith(Names.named("thingOne")).toInstance("THING ONE");
                bind(String.class).annotatedWith(Names.named("thingTwo")).toInstance("THING TWO");
            }
        }).injectMembers(box);
        System.out.println("thingOne after injection='"+box.thingOne+"'");
        System.out.println("thingTwo after injection='"+box.thingTwo+"'");
    }

    public static class Box {
        @Resource public String thingOne;
        @Resource public String thingTwo;
    }
}
是另一种选择(也是我推荐的一种,因为它仍然得到维护,并且提供了更好的模块化)。我已经添加了这个功能,所以应该很快就可以使用了。一旦它被使用,下面是它的使用方法:

@Grab(group='com.google.inject', module='guice', version='3.0')
@Grab(group='com.mycila.guice.extensions', module='mycila-guice-jsr250', version='3.6.ga')
import javax.annotation.Resource;
import com.google.inject.name.Names;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.mycila.guice.ext.jsr250.Jsr250Module;
import com.mycila.guice.ext.closeable.CloseableModule;

public class Main {
    public static void main(String[] args) {
        Box box = new Box();
        System.out.println("thingOne before injection='"+box.thingOne+"'");
        System.out.println("thingTwo before injection='"+box.thingTwo+"'");
        Guice.createInjector(new Jsr250Module(), new CloseableModule(), new AbstractModule() {
            @Override
            protected void configure() {
                bind(String.class).annotatedWith(Names.named("thingOne")).toInstance("THING ONE");
                bind(String.class).annotatedWith(Names.named("thingTwo")).toInstance("THING TWO");
            }
        }).injectMembers(box);
        System.out.println("thingOne after injection='"+box.thingOne+"'");
        System.out.println("thingTwo after injection='"+box.thingTwo+"'");
    }

    public static class Box {
        @Resource public String thingOne;
        @Resource public String thingTwo;
    }
}

最后,Netflix还支持@Resource连接,但连接起来有点笨重。这看起来是我能找到的最好的例子。

我认为您不能,因为Java注释没有指向它们所附加到的think的链接。