我遇到以下错误:
上下文初始化失败:org.springframework.beans.factory.BeanCreationException:创建名称为' contactService'的注册自动连接依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private uaiContacts.repository.ContactRepository uaiContacts.service.ContactService.contactRepository;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[uaiContacts.repository.ContactRepository]的匹配bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
这是产生错误的类:
package uaiContacts.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import uaiContacts.repository.ContactRepository;
import uaiContacts.model.Contact;
import uaiContacts.vo.ContactListVO;
@Service
@Transactional
public class ContactService {
@Autowired
private ContactRepository contactRepository;
@Transactional(readOnly = true)
public ContactListVO findAll(int page, int maxResults) {
Page<Contact> result = executeQueryFindAll(page, maxResults);
if(shouldExecuteSameQueryInLastPage(page, result)){
int lastPage = result.getTotalPages() - 1;
result = executeQueryFindAll(lastPage, maxResults);
}
return buildResult(result);
}
public void save(Contact contact) {
contactRepository.save(contact);
}
@Secured("ROLE_ADMIN")
public void delete(int contactId) {
contactRepository.delete(contactId);
}
@Transactional(readOnly = true)
public ContactListVO findByNameLike(int page, int maxResults, String name) {
Page<Contact> result = executeQueryFindByName(page, maxResults, name);
if(shouldExecuteSameQueryInLastPage(page, result)){
int lastPage = result.getTotalPages() - 1;
result = executeQueryFindByName(lastPage, maxResults, name);
}
return buildResult(result);
}
private boolean shouldExecuteSameQueryInLastPage(int page, Page<Contact> result) {
return isUserAfterOrOnLastPage(page, result) && hasDataInDataBase(result);
}
private Page<Contact> executeQueryFindAll(int page, int maxResults) {
final PageRequest pageRequest = new PageRequest(page, maxResults, sortByNameASC());
return contactRepository.findAll(pageRequest);
}
private Sort sortByNameASC() {
return new Sort(Sort.Direction.ASC, "name");
}
private ContactListVO buildResult(Page<Contact> result) {
return new ContactListVO(result.getTotalPages(), result.getTotalElements(), result.getContent());
}
private Page<Contact> executeQueryFindByName(int page, int maxResults, String name) {
final PageRequest pageRequest = new PageRequest(page, maxResults, sortByNameASC());
return contactRepository.findByNameLike(pageRequest, "%" + name + "%");
}
private boolean isUserAfterOrOnLastPage(int page, Page<Contact> result) {
return page >= result.getTotalPages() - 1;
}
private boolean hasDataInDataBase(Page<Contact> result) {
return result.getTotalElements() > 0;
}
}
这是存储库:
package uaiContacts.repository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import uaiContacts.model.Contact;
@Repository
public interface ContactRepository extends PagingAndSortingRepository<Contact, Integer> {
Page<Contact> findByNameLike(Pageable pageable, String name);
}