【问题标题】:Exclude JPA Spring boot - org.springframework.boot:spring-boot-starter-data-jpa' causes error in CRUDRepository when starting排除 JPA Spring boot - org.springframework.boot:spring-boot-starter-data-jpa' 在启动时导致 CRUDRepository 出错
【发布时间】:2023-03-12 01:06:01
【问题描述】:

我的计划是使用 1 个代码 2 个不同的应用程序。只有 1 个应用程序将连接到数据库并进行交易。在没有任何数据库的应用程序中启动我的 webapp 时出现错误,唯一的方法是排除 JPA 依赖项。启动应用程序时,它会抛出错误

chat.ChatService 中构造函数的参数 2 需要一个无法找到的“chat.ChatLogRepository”类型的 bean。

这是我的 Spring Boot 应用程序

@AllArgsConstructor
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class ApplicationDmz implements WebMvcConfigurer {

    public static void main(String[] args) {
        SpringApplication.run(chat.ApplicationDmz.class, args);
    }

    @Bean
    RestOperations restOperationsDmz() {
        return new RestTemplate();
    }

}

这是我调用 CRUDRepository 的服务

@AllArgsConstructor
@Component
public class ChatService {

  @Getter
  private SimpMessageSendingOperations template;

  SimpUserRegistry userRegistry;
  private ChatLogRepository chatLogRepository;

这是我的 CRUDRepository

import org.springframework.data.repository.CrudRepository;

public interface ChatLogRepository extends CrudRepository<ChatLog, Long> {

}

我只想在使用排除 JPA 启动我的应用程序时解决问题

【问题讨论】:

  • 您的应用程序应该有一个@SpringBootApplication 注释。如果您不想要 JPA,您确实必须排除依赖项,但您还必须确保您的服务不会被实例化,因为这是使用需要 JPA 可用的存储库。

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


【解决方案1】:

您的测试是手动导入您的存储库 bean:

import org.springframework.context.annotation.Import;

@Import(ChatLogRepository.class)

这应该在@Configuration 类或您的@SpringBootApplication 类中。

【讨论】:

  • 已经添加了@Import 但仍然遇到问题 rg.springframework.context.ApplicationContextException: Unable to start web server;嵌套异常是 org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean。
猜你喜欢
  • 2015-12-28
  • 2021-01-18
  • 2021-02-09
  • 2018-08-06
  • 2017-11-29
  • 2018-03-01
  • 1970-01-01
  • 2017-07-04
  • 2021-11-11
相关资源
最近更新 更多