1 回答

TA贡献1831条经验 获得超9个赞
玩框架使用 Guice:
https://www.playframework.com/documentation/2.7.x/JavaDependencyInjection https://github.com/google/guice/wiki/Motivation
您可以通过不同的方式实现它。最简单的例子:
1. 绑定注释
如果你只需要一个实现。https://www.playframework.com/documentation/2.7.x/JavaDependencyInjection#Binding-annotations
import com.google.inject.ImplementedBy;
@ImplementedBy(c1.class)
public interface i1 {
void m1();
}
2. 编程绑定
如果您需要同一类的一些实现。类似于限定符。你要求的那个。https://www.playframework.com/documentation/2.7.x/JavaDependencyInjection#Programmatic-bindings
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
public class Module extends AbstractModule {
protected void configure() {
bind(i1.class)
.annotatedWith(Names.named("c1"))
.to(c1.class);
bind(i1.class)
.annotatedWith(Names.named("c2"))
.to(c2.class);
}
}
代码的后面部分
@Inject @Named("c1")
i1 i;
添加回答
举报