spring 中构造函数的参数0需要一个找不到类型的Bean

pgky5nke  于 5个月前  发布在  Spring
关注(0)|答案(2)|浏览(84)

我在生产环境中运行我的spring Boot 应用程序时遇到问题。在本地环境下一切都正常。
这是我的错

Description:

Parameter 0 of constructor in com.tkg.product.service.impl.LocationService required a bean of type 'com.tkg.product.repository.CountryRepositoryInterface' that could not be found.

Action:

Consider defining a bean of type 'com.tkg.product.repository.CountryRepositoryInterface' in your configuration.

字符串
这是我的代码:

位置服务

package com.tkg.product.service.impl;

import com.tkg.product.entity.Country;
import com.tkg.product.entity.Destination;
import com.tkg.product.repository.CountryRepositoryInterface;
import com.tkg.product.repository.DestinationRepositoryInterface;
import com.tkg.product.service.LocationServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class LocationService implements LocationServiceInterface {

    private final CountryRepositoryInterface countryRepository;
    
    private final DestinationRepositoryInterface destinationRepository;

    @Override
    public List<Country> countries() {
        return countryRepository.findAll(Sort.by(Sort.Direction.ASC, "name"));
    }

    @Override
    public List<Destination> findDestinationByCountry(List<Long> countryIds) {
        return destinationRepository.findByCountryIdIn(countryIds);
    }
}

CountryRepositoryInterface

package com.tkg.product.repository;

import com.tkg.product.entity.Country;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;

@Component
public interface CountryRepositoryInterface extends JpaRepository<Country, Long> {
}


我试着用@Repository替换@Component,但一切都是一样的。
谢谢大家

tzcvj98z

tzcvj98z1#

使用基本包将@EnableJpaRepositories添加到主类

beq87vna

beq87vna2#

退房--> Spring @ComponentScan doesn't work on @Repository
从上面的链接:为了让spring知道哪个DataSource与哪个Repository相关,您应该在@EnableJpaRepositories annotation中定义它。

相关问题