【问题标题】:Problems with defining profiles in pom.xml (Spring Boot)在 pom.xml (Spring Boot) 中定义配置文件的问题
【发布时间】:2017-04-03 22:16:31
【问题描述】:

我有一个带有两个配置文件的 Spring Boot 应用程序:“dev”和“prod”。在“开发”配置文件中应该使用 H2 数据库,在“产品”中 - PostgreSQL。

它们在 pom.xml 中定义如下:

<profiles>
        <profile>
            <id>prod</id>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <scope>provided</scope>
                </dependency>

                <dependency>
                    <groupId>org.postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>9.4.1211.jre7</version>
                </dependency>

            </dependencies>
        </profile>

        <profile>
            <id>dev</id>
            <dependencies>
                <dependency>
                    <groupId>com.h2database</groupId>
                    <artifactId>h2</artifactId>
                </dependency>
            </dependencies>
        </profile>
</profiles>

pom.xml 中没有定义其他数据库。

在我的 application.properties 文件中,'prod' 配置文件被定义为活动:

spring.profiles.active = prod

此外,每个配置文件都有两个特定的属性文件:

应用程序-dev.properties:

spring.jpa.show-sql = true

和 application-prod.properties(我将我的应用部署到 AWS):

spring.datasource.url = jdbc:postgresql://${RDS_HOSTNAME}:${RDS_PORT}/${RDS_DB_NAME}
spring.datasource.username = ${RDS_USERNAME}
spring.datasource.password = ${RDS_PASSWORD}
spring.jpa.hibernate.ddl-auto = update

我使用以下 VM 选项启动我的 IntelliJ IDEA:

-Dspring.profiles.active="dev"

当我在本地启动程序时,我看到使用了“dev”配置文件(控制台中有消息“以下配置文件处于活动状态:dev”),但 H2 没有启动。我变成以下错误信息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Cannot determine embedded database driver class for database type NONE

Action:

        If you want an embedded database please put a supported one on the classpath.
    If you have database settings to be loaded from a particular profile you may need
to active it (the profiles "dev" are currently active).

请帮助我正确定义这些配置文件。

【问题讨论】:

    标签: java spring maven spring-boot


    【解决方案1】:

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

    spring.datasource.url = jdbc:h2:mem:yourDB
    spring.datasource.driverClassName = org.h2.Driver
    

    添加到application-dev.properties 文件中应该可以解决您的问题。

    来自Spring Boot documentation,在某些情况下可能需要:

    自 Spring 以来,您通常不需要指定驱动程序类名称 boot 可以从 url 推断出大多数数据库的情况。

    编辑

    Maven 配置文件不是 Spring Boot 配置文件。
    当你运行你的项目时,也使用 -P arg 启用 maven 配置文件:

      mvn spring-boot:run -Pdev -Dspring.profiles.active="dev"
    

    编辑

    处理生产环境问题的更新。

    为什么要覆盖 Spring 提供的 postgresql 的依赖版本?
    反正你用的postgresql驱动的版本不是org.postgresql.jdbc.Driver类而是org.postgresql.Driver。
    因此,请在 prod config 的属性文件中指定:

    spring.datasource.driverClassName = org.postgresql.Driver
    

    【讨论】:

    • 它给出以下错误:java.lang.IllegalStateException:无法加载驱动程序类:org.h2.Driver。而在“org.h2.Driver”部分中,“h2.Driver”是红色的
    • 您可以尝试在 IntelliJ 之外运行您的项目吗?看起来像副作用。您使用的是哪个 SpringBoot 版本?
    • 我正在使用 Spring Boot 版本 v1.4.1.RELEASE。据我了解,Maven 只是不添加数据库依赖项。
    • Maven 配置文件不是 Spring Boot 配置文件。我更新了我的答案。
    • 嗯,使用mvn spring-boot:run -Pdev -Dspring.profiles.active="dev" 它启动并工作。尽管测试未通过,但无法加载 ApplicationContext。
    猜你喜欢
    • 2020-09-23
    • 2020-01-23
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    相关资源
    最近更新 更多