将spring存储库移动到不同的文件夹

时间:2017-12-01 12:21:12

标签: java spring spring-data spring-data-jpa spring-data-elasticsearch

是否可以将spring数据repo实现移动到不同的文件夹(不是原始接口的相同或子文件夹)?如果我在同一个文件夹中有接口和实现,一切正常。如果我将其从com.app.domain.repo移至com.app.infrastr.repo,我会获得Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property methodName found for type RepoInterfaceName

更新

import com.app.domain.repo

public interface ARepo extends ElasticsearchRepository<A, String>, CustomizedARepo {
}

public interface CustomizedARepo {
    List<A> makeSearch(int x);
}

import com.app.infrastr.repo

public class CustomizedARepoImpl implements CustomizedARepo {

    private ElasticsearchTemplate elasticsearchTemplate;

    public CustomizedARepoImpl(ElasticsearchTemplate elasticsearchTemplate) {
        this.elasticsearchTemplate = elasticsearchTemplate;
    }

    @Override
    public List<A> makeSearch(int x){ return null; }
}

配置类是

@Configuration
@EnableElasticsearchRepositories(basePackages = {"com.app.domain.repo", "com.app.infrastr.repo"})
public class Config{}

错误是:

Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property makeSearch found for type ARepo

如果我将CustomizedARepoImpl移至com.app.domain.repo,则一切正常。

1 个答案:

答案 0 :(得分:0)

我找到了一个理由。 spring-data-commons中的文件getBasePackages内的方法RepositoryBeanDefinitionBuilder使你在这样的文件夹中实现

public Iterable<String> getBasePackages() {
            return Collections.singleton(ClassUtils.getPackageName(fragmentInterfaceName));
        }

如果我将其更改为

public Iterable<String> getBasePackages() {
            return configuration.getBasePackages();
        }

一切都按预期工作。将检查为什么春天的人这样做...