Весной я создаю MVC API с реализацией CrudRepository
и получаю UnsatisfiedDependencyException
.
Первый раз, когда я попробовал аннотацию @Autowired
, но не работает
Код контроллера:
package marcel.pirlog.licenta.userManagement.controllers;
import marcel.pirlog.licenta.userManagement.services.IAccountService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController("/account")
public class LoginController {
IAccountService accountService;
public LoginController(IAccountService accountService) {
this.accountService = accountService;
}
@RequestMapping(name = "/", method = RequestMethod.GET)
public ResponseEntity<String> getAll(){
return ResponseEntity.ok(accountService.findAll().toString());
}
}
Сервисный код:
package marcel.pirlog.licenta.userManagement.services;
import marcel.pirlog.licenta.userManagement.entities.AccountEntity;
import marcel.pirlog.licenta.userManagement.repositorys.IAccountRepository;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AccountService implements IAccountService {
private final IAccountRepository accountRepository;
public AccountService(IAccountRepository accountRepository) {
this.accountRepository = accountRepository;
}
@Override
public List<AccountEntity> findAll() {
List<AccountEntity> result = (List<AccountEntity>)accountRepository.findAll();
return result;
}
}
Код репозитория:
@Repository
public interface IAccountRepository extends CrudRepository<AccountEntity, Long> {
}
Получено UnsatisfiedDependencyException:
org.springframework.beans.factory.UnsatisfiedDependencyException: Ошибка создания бина с именем '/ account', определенным в файле [C: \ Users \ parlo \ Documents \ GitHub \ licenta \ ProiectLicenta \ Server \ user-management \ target \ classes \ marcel \ pirlog \ licenta \ userManagement\ controllers \ LoginController.class]: неудовлетворенная зависимость, выраженная через параметр конструктора 0;Вложенное исключение - org.springframework.beans.factory.UnsatisfiedDependencyException: ошибка создания бина с именем 'accountService', определенного в файле [C: \ Users \ parlo \ Documents \ GitHub \ licenta \ ProiectLicenta \ Server \ user-management \ target \ classes \marcel \ pirlog \ licenta \ userManagement \ services \ AccountService.class]: неудовлетворенная зависимость, выраженная через параметр конструктора 0;вложенное исключение: org.springframework.beans.factory.BeanCreationException: ошибка создания бина с именем 'IAccountRepository': сбой вызова метода init;вложенным исключением является java.lang.IllegalArgumentException: не управляемый тип: класс marcel.pirlog.licenta.userManagement.entities.AccountEntity ...