【问题标题】:Database config in multi module project多模块项目中的数据库配置
【发布时间】:2017-07-17 09:12:26
【问题描述】:

我把我的项目分成了多个模块,core负责数据库访问、业务逻辑等。还有一个模块提供了graphql API。 graphql 模块具有 core 作为其依赖项,但 core 模块中 application.yml 文件的配置似乎没有被加载。

设置

核心 - Application.class

package project.core;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
@SpringBootApplication
public class Application {

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

application.yml

spring:
  application:
    name: core  # Identify this application
  datasource:
    url: jdbc:postgresql://localhost:5432/project
    username: user
    password: something
    driver-class-name: org.postgresql.Driver
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQLDialect
    show-sql: true
    hibernate:
      ddl-auto: create

logging:
  level:
    org.springframework:
      web: TRACE
      security: TRACE

GRAPHQL 模块 - POM 层

<dependencies>
    <dependency>
        <groupId>project</groupId>
        <artifactId>core</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

Application.class

@EnableAutoConfiguration
@ComponentScan({"project.core", "project.graphql"})
public class ProjectGraphQL {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(ProjectGraphQL.class, args);
    }
}

core 添加到 @ComponentScan 修复了 JPA 的问题,但现在我收到以下错误消息。

***************************应用程序启动失败


说明:

无法确定数据库类型 NONE 的嵌入式数据库驱动程序类

我错过了什么?我相信我可以将配置复制到我的 graphql 配置中,但我宁愿将所有数据库内容排除在该模块之外。

【问题讨论】:

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


    【解决方案1】:

    您不应该以这种方式混合模块。 Spring Boot 在运行应用程序时执行的不仅仅是组件扫描。

    保持模块分离并使用应用程序上下文层次结构:

    @EnableAutoConfiguration
    @ComponentScan
    public class ProjectGraphQL {
        public static void main(String[] args) {
            new SpringApplicationBuilder()
                .parent(Application.class)
                .child(ProjectGraphQL.class)
                .run(args);
        }
    }
    

    【讨论】:

      【解决方案2】:

      我找到了一个解决方案,我告诉graphql 应用程序在哪里可以找到application.properties 文件(@PropertySource 不支持 yml)以及在哪里扫描JpaRepositoriesEntities,定义包@ComponentScan 还不够。

      @SpringBootApplication
      @PropertySource(value = {"core-application.properties", "application.properties"})
      @ComponentScan({"project.core", "project.graphql"})
      @EnableJpaRepositories("project.core")
      @EntityScan("project.core")
      @EnableTransactionManagement
      public class ProjectGraphQL {
          public static void main(String[] args) throws Exception {
              SpringApplication.run(ProjectGraphQL.class, args);
          }
      }
      

      我对额外的配置和切换到.properties 而不是.yml 并不满意,但至少现在可以使用。

      【讨论】:

        猜你喜欢
        • 2021-12-29
        • 2012-02-14
        • 2017-09-21
        • 1970-01-01
        • 1970-01-01
        • 2011-11-21
        • 2020-03-01
        • 1970-01-01
        • 2016-04-28
        相关资源
        最近更新 更多