gpt4 book ai didi

java - Guice/Morphia,辅助注入(inject)错误

转载 作者:行者123 更新时间:2023-12-01 11:55:13 25 4
gpt4 key购买 nike

(或者可能用法不正确?我是 Guice 的新手)我收到以下错误:

  • 字符串不能用作键;尚未完全指定。

  • java.lang.Class T 不能用作键;尚未完全指定。

我有以下内容:

public interface BaseRepository<T, PK> (){
public void f1();
}

public class BaseRepositoryMongoImpl<T, String> extends BaseDAO<T, String> implements BaseRepository<T, String> {
@Inject
BaseRepositoryMongoImpl(MongoClient mongoClient, Morphia morphia,
String dbName, @Assisted Class<T> type) {
super(type, mongoClient, morphia, (java.lang.String) dbName);
}

public void f1(){}
}

工厂接口(interface)和类

public interface BaseRepositoryFactory {
public BaseRepository create(Class<?> tableClass);
}

public class BaseRepositoryFactoryImpl implements BaseRepositoryFactory {

private final MongoClientProvider mongoClientProvider;
private final MorphiaProvider morphiaProvider;

@Inject
public BaseRepositoryFactoryImpl(MongoClientProvider mongoClientProvider,
MorphiaProvider morphiaProvider) {
this.mongoClientProvider = mongoClientProvider;
this.morphiaProvider = morphiaProvider;
}

@Override
public BaseRepository create(Class<?> tableClass) {
return new BaseRepositoryMongoDBImpl(mongoClientProvider.get(),
morphiaProvider.get(),
"DATABASE_NAME", //comes from properties
tableClass);
}
}

提供者类

public class MongoClientProvider implements Provider<MongoClient> {

@Override
public MongoClient get() {
MongoClient mongoClient = null;
try {
//for sake of simplicity, all properties code is removed
//the database name is part of the properties
mongoClient = new MongoClient("DATABASE_HOST");
} catch (UnknownHostException ex) {
Logger.error("Severe Error: Unknown database host", ex);
}

return mongoClient;
}
}

public class MorphiaProvider implements Provider<Morphia> {
@Override
public Morphia get() {
Morphia morphia = new Morphia();
//for sake of simplicity, all properties code is removed
//the package name is part of the properties
morphia.mapPackage("MODEL_PACKAGE", true);
return morphia;
}
}

最后是模块类

public class DatabaseModule extends AbstractModule {
@Override
protected void configure() {
install(new FactoryModuleBuilder()
.implement(BaseRepository.class, BaseRepositoryMongoDBImpl.class)
.build(BaseRepositoryFactory.class));
}
}

我需要在模块类中使用 TypeLiteral 进行任何操作吗?

另外,我可以将 BaseRepositoryFactory 变量声明为

@Inject
private static BaseRepositoryFactory baseRepo;

提前致谢!

最佳答案

首先:你不能注入(inject)一个普通的字符串,guice只是不知道你想要注入(inject)什么。使用限定符来指定您需要什么。最简单的修饰符:@Named。

所以:

@Inject
@Named("foo")
private String myString;

@Provides
@Named("foo")
public String myString() {
return ... whatever myString should be ... constant? property? DB value?
}

第二:您的辅助注入(inject)方法不是“guicy”方式。查看assisted inject extension ,您不必自己实现接口(interface):

install(new FactoryModuleBuilder()
.implement(Payment.class, RealPayment.class)
.build(PaymentFactory.class));

这样,您就可以删除一些代码,从而使搜索真正的问题变得更加容易。

第三:您可以使用requestStaticInjection() ,但通常你不应该这样做。依赖注入(inject)和静态值持有者的概念不匹配,你会遇到很多麻烦,只需使用单例绑定(bind)即可。

关于java - Guice/Morphia,辅助注入(inject)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28509608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com