简体   繁体   English

Spring Mongo-从我的自定义存储库实现访问存储库接口方法

[英]Spring Mongo - Accessing repository interface methods from my custom repository implementation

I am defining a Spring Mongo repository interface as follows: 我正在定义一个Spring Mongo存储库接口,如下所示:

public interface FooRepository
  extends MongoRepository<Foo, String>, FooRepositoryCustom {
  Foo findByMyField(String myField);
}

... and a custom interface like this: ...和这样的自定义界面:

public interface FooRepositoryCustom {
  Foo findByMyObject(MyObject obj);
}

In my custom repository implementation, I would like to define findByMyObject by calling the findByMyField method. 在我的自定义存储库实现中,我想通过调用findByMyField方法来定义findByMyObject。 Something like: 就像是:

public class FooRepositoryImpl implements FooRepositoryCustom {
  public Foo findByMyObject(MyObject obj) {
    return new Foo(<repository>.findByMyField(obj.getId()));
  }
}

Any clue of how I could get a reference to that repository interface? 关于如何获取该存储库接口的引用的任何线索?

You can try the following ... 您可以尝试以下...

public class FooRepositoryImpl implements FooRepositoryCustom {
@Autowired
private FooRepository fooRepository;
public Foo findByMyObject(MyObject obj) {
    return new Foo(fooRepository.findByMyField(obj.getId()));
  }
}

You can also try 您也可以尝试

@Autowired
private MongoRepository<Foo, String> fooRepository;

Not sure if the generics will trip the autowire by type. 不确定泛型是否会按类型触发自动接线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM