注入从豆继承的界面

发布于 2025-02-09 08:00:53 字数 1830 浏览 2 评论 0原文

我正在尝试创建一个自定义的MongoDB存储库,该存储库将处理仅与业务上下文相关的交易。

由于我想公开这些函数,因此我遇到了一些麻烦,试图在服务上注入界面而不是实际实现,这是ATM的外观:

接口:接口:

public interface MongoRepositoryActions<T> {
    <S extends T> S save(S entity);
}

实现

import com.mongodb.client.MongoClient;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

@ApplicationScoped
public class BaseMongoRepository<T> implements MongoRepositoryActions<T> {

    @Inject
    MongoClient mongoClient;

    public <O extends T> O save(O object) {
        // Do some stuff related to the business.
        insertOperation(object);
        return object;
    }
}

这是我无法弄清楚如何弄清楚如何的使用它。 我正在尝试公开的接口:

public interface FruitRepository extends MongoRepositoryActions<Fruit> {

}

具有存储库的服务层:

import com.abinbev.b2b.core.MongoRepositoryActions;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

@ApplicationScoped
public class FruitService {

    @Inject
    MongoRepositoryActions<Fruit> fruitRepository; // This works fine

    @Inject
    FruitRepository repository; // This one doesn't

    public Fruit saveFruit(Fruit fruit) {
        return fruitRepository.save(fruit);
    }
}

因此,如果我尝试通过注入界面来运行应用程序,我会收到此错误:

javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type com.abinbev.b2b.core.fruit.FruitRepository and qualifiers [@Default]

我也尝试注释界面,但没有也工作。

也许我错过了一些东西。 可以提供更多信息! 如有必要,我还可以在我的GitHub帐户上推出一个可复制的示例。

I'm trying to create a custom MongoDB Repository that will handle transactions that are related to the business context only.

Since I want to expose these functions trough a interface, I'm having some trouble trying to inject the Interface on the service instead of the actual implementation, here's how it looks like ATM:

Interface:

public interface MongoRepositoryActions<T> {
    <S extends T> S save(S entity);
}

Implementation

import com.mongodb.client.MongoClient;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

@ApplicationScoped
public class BaseMongoRepository<T> implements MongoRepositoryActions<T> {

    @Inject
    MongoClient mongoClient;

    public <O extends T> O save(O object) {
        // Do some stuff related to the business.
        insertOperation(object);
        return object;
    }
}

Here's where I can't figure out how to use it.
The interface that I'm trying to expose:

public interface FruitRepository extends MongoRepositoryActions<Fruit> {

}

The service layer that has the repository:

import com.abinbev.b2b.core.MongoRepositoryActions;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

@ApplicationScoped
public class FruitService {

    @Inject
    MongoRepositoryActions<Fruit> fruitRepository; // This works fine

    @Inject
    FruitRepository repository; // This one doesn't

    public Fruit saveFruit(Fruit fruit) {
        return fruitRepository.save(fruit);
    }
}

So if I try to run the application by injecting the interface, I get this error:

javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type com.abinbev.b2b.core.fruit.FruitRepository and qualifiers [@Default]

I've also tried to annotate the interface, but didn't work as well.

Maybe I'm missing something.
Further information can be provided!
If necessary I can also push a reproducible example on my Github account.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文