java - Spring 启动: Configuration Scan missing classes due to "weird" setup

标签 java spring spring-boot spring-data-jpa

我们以一种困惑的方式设置了一个 ETL 项目,只是为了完成工作:

该项目依赖于其他几个 Spring 项目,因此我们将这些项目作为依赖项包含在内。这些项目包含自己的 @Configuration 类,可能会导致冲突。我现在的状态是在我的 ETL 项目中设置动态存储库,但它没有 Autowiring ,引发错误:

constructor in com.myetl.service.impl.ErrorServiceImpl required a bean of type 'com.myetl.repository.ProjectErrorRepository' that could not be found.

当我启动应用程序时,我之前在控制台中看到它将其视为 Redis 存储库:

RepositoryConfigurationExtensionSupport : Spring Data Redis - Could not safely identify store assignment for repository candidate interface com.myetl.repository.ProjectErrorRepository. If you want this repository to be a Redis repository, consider annotating your entities with one of these annotations: org.springframework.data.redis.core.RedisHash (preferred), or consider extending one of the following types with your repository: org.springframework.data.keyvalue.repository.KeyValueRepository.

我的基础项目中没有 Redis 配置,但它肯定存在于我的依赖项之一中。

我尝试了一些不同的方法,但没有任何效果。例如,使用排除过滤器:

@SpringBootApplication(exclude = {DifferentProjectConfiguration.class})
@ComponentScan(includeFilters =
               @ComponentScan.Filter(type= FilterType.REGEX,
                                     pattern = "com\\.etl\\..*"),
               excludeFilters =
               @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE,
                                     value = {OtherProjectRedisConfiguration.class}))
public class ProjectsMigrationApplication {
...

如果我注释掉存储库,应用程序可以正常加载,包括 Controller 和服务 bean。这个问题似乎只针对我想要成为 MySql JPA 存储库的存储库。这是我的存储库的定义(我尝试过使用和不使用 @Repository 注释):

package com.etl.repository;

import com.etl.domain.ProjectError;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

public interface ProjectErrorRepository extends PagingAndSortingRepository<ProjectError, Long> {

}

@Entity
@Table(name="project_error")
@Data
public class ProjectError {
    @Id
    @Column(name = "project_error_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column
    private String errMsg;

    @Column(name = "user_email")
    private String userEmail;
@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "migrationEntityManagerFactory", basePackageClasses = {
        ProjectError.class}, repositoryBaseClass = ProjectErrorRepository.class)
public class ProjectMigrationJpaConfig {

    @Bean(name = "migrationEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean migrationEntityManagerFactory(EntityManagerFactoryBuilder builder,
            DataSource dataSource) {
        return builder
                .dataSource(dataSource)
                .packages(ProjectError.class)
                .build();
    }

}

在迷宫般的项目中诊断此类错误以了解 Spring 在幕后所做的事情的最佳方法是什么?

最佳答案

听起来您正在将 Redis 作为来自另一个项目的传递依赖项。您可能会遇到的是自动配置。

尝试从您的项目中排除 Redis 自动配置,看看会发生什么。

    @SpringBootApplication(exclude = { RedisAutoConfiguration.class, RedisRepositoriesAutoConfiguration.class })

关于java - Spring 启动: Configuration Scan missing classes due to "weird" setup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60860211/

相关文章:

java - 如何修复 Spring Boot 应用程序中的 Websockets 和 Shedlock 兼容性

java - 如何使用 Java 中断 Selenium 线程

java - 当我在主函数中声明该数组时,如何用函数中的对象填充数组?

java - 如何使用 onSaveInstanceState 保存复选框状态

java - springdoc-openapi-ui OAuth 2.0 授权代码流与 PKCE

java - Spring Boot:包中的类未加载

java - POI,颜色行不起作用

java - 使用 Spring Security 保护应用程序不起作用

java - 在 Spring Mvc Java 中记录未处理的异常

Spring Cloud : default redirecting from Gateway to UI