如何将依赖项注入到存储库基类中 [英] How to inject a dependency into a repository base class

查看:124
本文介绍了如何将依赖项注入到存储库基类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Data的各种 @EnableXXXRepository 批注允许您为存储库指定一个自定义基类,它将用作存储库中方法的实现。

The various @EnableXXXRepository annotations of Spring Data allow you to specify a custom base class for your repositories, which will be used as an implementation of methods in your repository.

如果这样的基类需要访问 ApplicationContext 中的其他bean,那么如何注入它们呢?它不是开箱即用的,因为Spring Data会实例化那些基类本身,仅支持依赖于存储的特殊构造函数参数。

If such a base class needs access to other beans in the ApplicationContext how does one get those injected? It doesn't work out of the box, because Spring Data instantiates those base classes itself, supporting only special store dependent constructor parameters.

注意:我创建了这个在聊天中回答此已删除的问题,并认为这可能对

Note: I created this answer in the chat to this now deleted question and thought it might be valuable for others, although the original question is gone.

推荐答案

@Enable中。 .Repository 批注指定 repositoryBaseClass repositoryFactoryBeanClass 。像这样:

In the @Enable...Repository annotation specify a repositoryBaseClass and repositoryFactoryBeanClass. Like this:

@EnableMongoRepositories(
    repositoryBaseClass = MyBaseClass.class,
    repositoryFactoryBeanClass = MyRepositoryFactoryBean.class)

在那 RepositoryFactoryBean 类,可以使用普通的依赖注入,因为它是Spring Bean,因此,例如,您可以获取 SomeBean 通过构造函数注入,如下所示:

In that RepositoryFactoryBean class, you can use normal dependency injection, because it is a Spring Bean, so, for example, you can get an instance of SomeBean injected via the constructor, as shown below:

public class MyRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends MongoRepositoryFactoryBean<T,S,ID>{

    private final SomeBean bean;

    public MyRepositoryFactoryBean(Class repositoryInterface, SomeBean bean) {
        super(repositoryInterface);
        this.bean = bean;
    }

}

您的 RepositoryFactoryBean 现在通过覆盖'getFactoryInstance'创建自定义 RepositoryFactory 的实例。

Your RepositoryFactoryBean now create an instance of a custom RepositoryFactory by overwriting 'getFactoryInstance'.

@Override
protected RepositoryFactorySupport getFactoryInstance(MongoOperations operations) {
    return new MyMongoRepositoryFactory(operations, bean);
}

这样做时,它可以传递要注入的bean。在上面的示例中,是 bean

While doing so, it can pass on the bean to be injected. bean in the example above.

最后,该工厂实例化了存储库基类。可能最好的方法是将所有内容委托给现有的工厂类,然后将依赖项注入到混合中:

And this factory finally instantiates your repository base class. Probably the best way to do it is to delegate everything to the existing factory class and just add injecting of the dependency to the mix:

public class MyMongoRepositoryFactory extends MongoRepositoryFactory {

    private final SomeBean bean;

    MyMongoRepositoryFactory(MongoOperations mongoOperations, SomeBean bean) {
        super(mongoOperations);
        this.bean = bean;
    }

    @Override
    protected Object getTargetRepository(RepositoryInformation information) {
        Object targetRepository = super.getTargetRepository(information);
        if (targetRepository instanceof MyBaseClass) {
            ((MyBaseClass) targetRepository).setSomeBean(bean);
        }
        return targetRepository;
    }
}

有一个在Github上完整的工作示例

这篇关于如何将依赖项注入到存储库基类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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