在使用Java / Sping Boot 的应用程序上工作。我们有两个存储库,一个应用程序和一个“微服务”。在本地环境中运行这两个都很好。在staging中,我得到一个
第一个月
当应用程序试图调用微服务时。
查看微服务日志,我看到以下内容:
Error creating bean with name 'service' defined in URL [jar:file:/workspace/<repository>/lib/<repository>-plain.jar!<service>.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'repository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
字符串
和
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in <service> required a bean of type <repository> that could not be found.
Action:
Consider defining a bean of type <repository> in your configuration.
型
问题是,我不明白1)为什么应用程序在本地环境中启动良好,但不能暂存,2)是什么导致了“required a bean of type”问题。我有其他服务/repo连接定义了相同的方式,工作正常。下面的代码示例:
不起作用的服务:
@Service
@Slf4j
@AllArgsConstructor
public class NotWorkingService {
private NotWorkingRepository repository;
型
无法工作的存储库:
public interface NotWorkingRepository extends JpaRepository<Entity, Long> {
public List<Entity> findAllByName(String name);
}
型
这里有一个服务,工作:
@Service
@Slf4j
@AllArgsConstructor
public class WorkingService {
private WorkingRepository repository;
型
它的repository:
public interface WorkingRepository extends JpaRepository<Entity, Long> {
Optional<Entity> findBySerialNumber(String serialNumber);
型
他们看起来完全一样,所以我很困惑,到底是什么问题。
我已经看了多个现有的问题,但没有一个答案似乎适用。我不应该需要@Repository,因为我已经有extends JpaRepository
,我没有任何@Autowired在工作或不工作的一个,我有@Service注解,目录结构是正确的,我只看到一个应用程序.jar文件。
我会很感激这里的任何想法。
下面是软件包的结构(我不能共享文件名,但是上面的打开目录是工作目录,下面的是非工作目录。下面的文件是@SpringBootApplication:
package structure
我能看到的唯一区别是BaseInterface文件?这是内容:
public interface BaseInterface {
// This class functions as a basePackageClass reference for AppJpaConfiguration
}
型
也许这与问题有关?但是,应用程序中的其他工作服务/存储库连接没有BaseInterface。
下面是文件结构的折叠版本:
collapsed package structure的
1条答案
按热度按时间zte4gxcn1#
如果repository interface位于
@EnableJpaRepositories
annotation中指定的basePackages
或其子包之外,Spring Data JPA将无法发现和管理该repository。basePackages
属性确定用于扫描repository interface的根包。例如,考虑以下配置:
字符串
在此设置中,Spring Data JPA扫描“com.example.repository”包及其子包以查找repository接口。如果repository接口位于不同的包中,例如
com.example.other
,则不会发现它:型
要确保发现所有相关的存储库接口,请将接口移动到指定
basePackages
内或下的包中。或者,您可以使用
@EnableJpaRepositories
的basePackageClasses
属性:型
这里,
RepositoryMarkerClass
是com.example.repository
包中的一个标记类,Spring Data JPA使用它的包进行存储库扫描:型
这确保了所有存储库接口,即使是
basePackages
外部的接口,都可以被Spring Data JPA发现和管理。