spring 上下文未加载

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


DataJpa存储库:


@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);

    }

}

参观服务:


@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);

    }

}



一只萌萌小番薯
浏览 101回答 2
2回答

海绵宝宝撒

尝试将您的存储库包添加到@ComponentScan配置中@ComponentScan({"com.java.library.service",&nbsp;"com.java.library.repository"})

喵喔喔

尝试在 VisitService 类中添加 @Autowired 注解。@Autowiredprivate VisitRepositoryImpl repository;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java