【问题标题】:Why `SpringBootApplication` does not scan `Entites` and `Repositories` when custom jpa `@Configuration`为什么自定义jpa`@Configuration`时`Spring Boot Application`不扫描`Entities`和`Repositories`
【发布时间】:2021-04-04 03:53:54
【问题描述】:

在我的 library 中,我有一些 EntitiesRepositories 我想将它们包含在我的 spring-boot 项目中。

这是来自library@Confituration 类的示例

package com.mylibrary.config

@Configuration
@ComponentScan("com.mylibrary.service")
@EntityScan("com.mylibrary.repo")
@EnableJpaRepositories("com.mylibrary.repo")
@ConditionalOnClass(value = [DataSource::class])
open class MyCustomConfigFromLibrary

这样我尝试在我的 spring-boot 项目中包含 MyCustomConfigFromLibrary

package com.mainapp

@SpringBootApplication
@Import(value = [MyCustomConfigFromLibrary::class])
class Application

但是当我运行我的 spring-boot 应用程序时,它不想从我的 spring-boot 项目本身扫描 RepositoriesEntities。它扫描它的Components,但不扫描它的RepositoriesEntitiesRepositoriesEntities 它只扫描来自我的library

所以,为了解决这个问题,我找到了我 do not 喜欢的解决方案!我只是明确地将@EntityScan@EnableJpaRepositories 添加到我的Application 类中。

package com.mainapp

@SpringBootApplication
@Import(value = [MyCustomConfigFromLibrary::class])
@EntityScan
@EnableJpaRepositories
class Application

【问题讨论】:

  • 您可以尝试将spring自动配置添加到您的库中,然后将其作为spring starter添加到您的项目中。

标签: java spring spring-boot kotlin


【解决方案1】:

据我所知,如果您使用自己的自定义 schema.sql 和/或 data.sql,那么 Spring Boot 不会检测到 JPA Repos 或实体。如果您希望您的应用程序扫描这些类,请尝试从 resources 文件夹中删除自定义 schema.sql/data.sql

在基于 JPA 的应用程序中,您不应同时使用 spring.jpa.hibernate.ddl-autoschema.sql。如果您使用 schema.sql,请务必禁用 spring.jpa.hibernate.ddl-auto。 p>

参考 - https://walkingtechie.blogspot.com/2018/12/execute-schema-and-data-sql-on-startup-spring-boot.html

【讨论】:

    猜你喜欢
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    相关资源
    最近更新 更多