春天的上下文不加载

时间:2019-09-08 16:08:44

标签: java spring spring-boot

我尝试加载上下文,但是得到UnsatisfiedDependencyException 我不明白是什么原因,提供了服务中的注释和构造函数

DataJpaRepository:

@Transactional(readOnly = true)
public interface DataJpaRepository extends JpaRepository<Visit, Long> {

    @Override
    @Transactional
    Visit save(Visit visit);

    Visit findByGuestId(long guestId);

    @Query("SELECT DISTINCT v FROM Visit v WHERE  v.date BETWEEN :from AND :to")
    List<Visit> getBetween(@Param("from") Date from, @Param("to") Date to);
}


@Repository
public class VisitRepositoryImpl implements VisitRepository {

    @Autowired
    private DataJpaRepository dataJpaRepository;


    @Override
    public Visit get(long guestId) {
        return dataJpaRepository.findByGuestId(guestId);
    }

    @Override
    public Visit save(Visit visit) {
        return dataJpaRepository.save(visit);
    }

    @Override
    public List<Visit> getAll() {
        return dataJpaRepository.findAll();
    }

    @Override
    public List<Visit> getBetween(Date from, Date to) {
        Objects.requireNonNull(from, "fromDate must not be null");
        Objects.requireNonNull(to, "toDate must not be null");
        return dataJpaRepository.getBetween(from, to);
    }
}

VisitService:

@Service
public class VisitService {

    private VisitRepositoryImpl repository;

    @Autowired
    public VisitService(VisitRepositoryImpl repository) {
        this.repository = repository;
    }

    public Visit save(Visit visit) {
        return repository.save(visit);
    }

    public List<Visit> getAll() {
        return repository.getAll();
    }

    public List<Visit> getBetween(Date from, Date to) {
        Assert.notNull(from, "fromDate must not be null");
        Assert.notNull(to, "toDate  must not be null");
        return repository.getBetween(from, to);
    }
}

我收到由以下原因引起的异常

  

org.springframework.beans.factory.UnsatisfiedDependencyException:   在文件中定义名称为“ visitService”的bean创建时出错   [D:\ JAVA \ library-project \ target \ classes \ com \ java \ library \ service \ VisitService.class]:   通过构造函数参数0表示的不满足的依赖关系;   嵌套异常为   org.springframework.beans.factory.NoSuchBeanDefinitionException:否   类型的合格豆   'com.java.library.repository.VisitRepositoryImpl'可用:预期   至少1个符合自动装配候选条件的bean。相依性   注释:{}

主班也:

@EnableJpaRepositories("com.java.library.repository")
@EntityScan("com.java.library.model")
@ComponentScan("com.java.library.service")
@SpringBootApplication
public class LibraryApplication {

    public static void main(String[] args) {
        SpringApplication.run(LibraryApplication.class, args);
    }

}

可能是什么问题?

2 个答案:

答案 0 :(得分:4)

尝试将您的存储库软件包添加到@ComponentScan配置中

@ComponentScan({"com.java.library.service", "com.java.library.repository"})

答案 1 :(得分:-2)

尝试在VisitService类中添加@Autowired注释。

@Autowired
private VisitRepositoryImpl repository;