我在生产环境中运行我的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,但一切都是一样的。
谢谢大家
2条答案
按热度按时间tzcvj98z1#
使用基本包将@EnableJpaRepositories添加到主类
beq87vna2#
退房--> Spring @ComponentScan doesn't work on @Repository
从上面的链接:为了让spring知道哪个DataSource与哪个Repository相关,您应该在@EnableJpaRepositories annotation中定义它。