启动时出现Spring Boot UnsatisfiedDependencyException

时间:2019-12-22 17:06:47

标签: java spring spring-boot spring-mvc exception

我在春季通过CrudRepository实现创建了一个MVC API,并收到了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”的bean时出错   [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”的bean创建时出错   [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'的bean:init的调用   方法失败;嵌套的异常是java.lang.IllegalArgumentException:   不是托管类型:类   marcel.pirlog.licenta.userManagement.entities.AccountEntity ...

3 个答案:

答案 0 :(得分:0)

正如评论中提到的,在您的@Entity上使用AccountEntity

装有

    @RestController("/account")
    public class LoginController {

尝试

    @RestController
    @RequestMapping("account")
    public class LoginController {

答案 1 :(得分:-1)

向我们展示AccountEntity类。是否带有注解@Entity,您是否有Long类型的ID。应该是这样的:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
    public class AccountEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    public Long getId() { return id; }

    public void setId(Long id) { this.id = id; }

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }
}

答案 2 :(得分:-1)

尝试添加@EnableJpaRepositories(“ your.package.name”)并确保您具有@ComponentScan(“ base.package.name”)到SpringInitializer。