启动我的应用程序时遇到问题。有人可以帮我解决这个问题吗?
Parameter 0 of constructor in com.journaldev.elasticsearch.service.BookServiceImpl required a bean of type 'com.journaldev.elasticsearch.dao.search.BookRepositorySearch' that could not be found.
Action:
Consider defining a bean of type 'com.journaldev.elasticsearch.dao.search.BookRepositorySearch' in your configuration.
通用存储库
public interface GenericRepository<T, K> {
Map<String, Object> get(final K id);
}
通用存储库实现
public class GenericRepositoryImpl<T, K extends Serializable> implements GenericRepository<T, K> {
private RestHighLevelClient restHighLevelClient;
private ObjectMapper objectMapper;
public GenericRepositoryImpl(ObjectMapper objectMapper, RestHighLevelClient restHighLevelClient) {
this.objectMapper = objectMapper;
this.restHighLevelClient = restHighLevelClient;
}
@Override
public Map<String, Object> get(K id) {
return null;
}
}
书库搜索
@Component
public interface BookRepositorySearch extends GenericRepository<Book, Long> {}
图书服务
public interface BookService {
Map<String, Object> get(final Long id);
}
BookServiceImpl
@Service
public class BookServiceImpl implements BookService {
private final BookRepositorySearch bookRepositorySearch;
public BookServiceImpl(BookRepositorySearch bookRepositorySearch) {
this.bookRepositorySearch = bookRepositorySearch;
}
@Override
public Map<String, Object> get(Long id) {
return null;
}
}
原文由 Aymen Kanzari 发布,翻译遵循 CC BY-SA 4.0 许可协议
从您之前的评论来看,您似乎想保留
BookRepositorySearch
作为接口。如果是这种情况,您需要创建该接口的具体实例并将@Component
放在上面。您不需要
@Component
在您的接口声明中,您不能在接口中扩展类。创建一个实现接口并扩展 extends
GenericRepository<Book, Long>
的具体类型,你想自动装配并将@Component
放在上面:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-class-ctor