spring-boot 数据源配置文件 w/ application.properties

2024-02-01

根据反馈更新了问题:

我有一个 spring-boot 应用程序,它具有三个数据库:用于集成测试的 H2,以及用于质量保证和生产的 Postgresql。由于 spring-boot 为您创建了默认数据源,因此我没有为集成测试定义任何内容。我想我会使用 application.properties 来定义我的数据源连接值,但我不确定处理这个问题的最佳方法是什么。

我有两个文件:

src/main/resources/application.properties

spring.profiles.active=production
appName = myProduct
serverPort=9001

spring.datasource.url=jdbc:postgresql://localhost/myDatabase
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driverClassName=org.postgresql.Driver

spring.jpa.hibernate.hbm2ddl.auto=update
spring.jpa.hibernate.ejb.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.hibernate.show_sql=true
spring.jpa.hibernate.format_sql=true
spring.jpa.hibernate.use_sql_comments=false
spring.jpa.hibernate.type=all
spring.jpa.hibernate.disableConnectionTracking=true
spring.jpa.hibernate.default_schema=dental

src/main/resources/application-test.properties

spring.profiles.active=test
serverPort=9002

spring.datasource.url = jdbc:h2:~/testdb
spring.datasource.username = sa
spring.datasource.password = 
spring.datasource.driverClassName = org.h2.Driver

liquibase.changeLog=classpath:/db/changelog/db.changelog-master.sql

我曾经使用 gradle (使用“gradle build test”)或在 IntelliJ 中运行测试。我更新了我的 gradle 文件以使用:

task setTestEnv {
    run { systemProperty "spring.profiles.active", "test" }
}

但当我跑步时gradle clean 构建 setTestEnv 测试我收到的错误似乎表明测试正在尝试连接到实际的 Postgresql 数据库:

Caused by: org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:138)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
    at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22)
    at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:32)
    at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:393)
    at org.postgresql.Driver.connect(Driver.java:267)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:278)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:182)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:701)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:635)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:486)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:144)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:116)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:103)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:127)
    at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:288)
    ... 42 more
Caused by: java.net.ConnectException: Connection refused
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)

我还没有弄清楚如何在 IntelliJ 中设置默认的 system.property == "test"...


参见部分21.3 http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-profile-specific-propertiesSpring Boot 文档的一部分。本节介绍如何定义使用以下格式的配置文件特定属性文件应用程序-{profile}.properties。这可以帮助您隔离每个配置文件的属性。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

spring-boot 数据源配置文件 w/ application.properties 的相关文章

随机推荐

  • 如何向 ExtJs 渲染的 html 添加 data- 属性?

    使用 ExtJs 4 1 我正在创建一个面板 例如 我希望生成的 html 包含一个或多个 data 属性 例如 data intro some text data step 1 如何才能做到这一点 组件呈现后 您可以将属性应用到表示组件的
  • Yarn 安装抛出错误:gyp 动词 `which` 失败错误:未找到:python2

    在 React 项目中 我尝试运行 yarn install 但它抛出以下错误 吉卜赛人 动词which失败错误 未找到 python2 完整错误回溯 yarn install v1 22 4 warning package lock js
  • 后台位置跟踪:iOS

    我正在尝试在苹果的重要位置更改服务和自己定期启动 停止位置管理器之间做出选择 苹果对此是这么说的 收集位置数据是一项耗电的操作 它涉及打开机载无线电并查询可用的手机信号塔 Wi Fi 热点或 GPS 卫星 这可能需要几秒钟的时间 让标准定位
  • 如何使用 AppleScript 将窗口移动到某个桌面?

    我在桌面 1 OS X Lion 上有一个应用程序 窗口 我想将其移动到桌面 3 关于如何执行此操作有什么见解吗 没什么大不了的 但如果可能的话 我想确保有 3 个且只有 3 个可用的桌面 这个答案 https stackoverflow
  • 在Python中用另一个子列表替换一个子列表

    我想替换列表中的子列表a 还有另一个子列表 像这样的事情 a 1 3 5 10 13 假设我想要一个子列表 例如 a sub 3 5 10 并将其替换为 b sub 9 7 所以最终结果将是 print a gt gt gt 1 9 7 1
  • 角度$resource删除不会将正文发送到express.js服务器

    hye 我正在服务器端使用 angular js 和 node js Express js 构建一个应用程序 由于某种原因 我在处理删除请求时遇到问题 没有人到达服务器端 这是我的 angular js 资源代码 scope deleteP
  • Rails:带有参数的 rake 任务不起作用

    这是我的耙子任务 namespace users do task change role role gt environment do t args puts args role end end 我这样称呼它 rake users chan
  • 什么是图形上下文? (iOS)

    图形上下文到底是什么 当使用 Core Graphic 绘图时 我们会获得对上下文的引用 当我查看文档时 它似乎是一个对象 负责处理正确的绘图 无论是用于打印 设备 pdf 等 谁能帮助我理解上下文到底是什么 我尝试阅读文档 但我不明白 它
  • Oracle 协议适配器错误

    你好 我刚刚在我的笔记本电脑上安装了 Oracle 10 g Express Edition 平台是Windows XP SP3 安装正常 但是 当我尝试通过 SqlPlus 连接到 Oracle 数据库时 出现 TNS 协议适配器错误 我
  • ServicePointManager.SecurityProtocol 如何工作?

    我想知道楼盘怎么样ServicePointManager SecurityProtocol当我设置三个不同的时有效SecurityProtocolType在她的旗帜上 IE ServicePointManager SecurityProto
  • 什么是“git config user.password”?

    我见过git config user password在几个 Stack Overflow 答案中推荐作为保存 git 用户凭据的方法 如何在 Git 中保存用户名和密码 https stackoverflow com a 54979082
  • ESLint 错误 no-unneed-ternary

    ESLint 在我的 JS 模块中告诉我这个错误消息 错误 no unneeded ternary 不必要地使用条件表达式进行默认赋值 错误出现在get方法上的return陈述return val val defaultVal import
  • Auth0。如何获取访问令牌中用户的权限?

    如何使用 Auth0 的核心授权功能 在第一个授权请求中 获取访问令牌中的用户权限 我需要所有权限的数组以及有关用户的其他信息 附 我已经创建了角色 权限 并将角色分配给用户 在 API 设置中的访问令牌切换和 RBAC 中启用添加权限 这
  • 讲解extjs的MVC架构

    我使用 Javascript 创建了一个小型数独应用程序 现在我正在尝试将该 javascript 代码转换为 extjs 4 1 1a 代码 我已经经历了docs http docs sencha com ext js 4 1 guide
  • 使用jquery添加两个变量

    var a 1 var b 2 var c a b c将显示为12 但是我需要3 我如何使用 jQuery 做到这一点 绝对使用 jQuery 因为 jQuery 的威力显然是无与伦比的 下面是如何使用 100 jQuery var a 1
  • 如何使用R做多项选择题?

    我试图弄清楚如何分析我最近进行的一项调查中的多个选择 多个响应 即 选择所有适用的 问题 SPSS 具有分析在线调查数据和此类问题的出色功能 因此我猜测 R 具有该功能以及更多功能 在 Excel 中处理这些调查答案有点棘手 例如 按年龄显
  • Pathauto 中的菜单路径

    如何在 Drupal 7 下获取 pathauto 以通过完整菜单路径生成 URL 别名 只是一个更新 以防有人在使用更新版本的 Pathauto Token 时遇到此问题 这对我有用 节点 菜单链接 父母 加入路径 节点 菜单链接
  • 为什么0=0.5?

    我注意到 Z3 4 3 1 在处理 smt2 文件时出现一些奇怪的行为 If I do assert 0 0 5 就会得到满足 但是 如果我改变顺序并执行 assert 0 5 0 这是不能令人满意的 我对发生的情况的猜测是 如果第一个参数
  • pandas:将多列转换为字符串

    我有一些专栏 a b c etc a and c are float64 while b is object 我想将所有列转换为字符串并保留nans 尝试使用df a b c df a b c astype str 但这留下了空白float
  • spring-boot 数据源配置文件 w/ application.properties

    根据反馈更新了问题 我有一个 spring boot 应用程序 它具有三个数据库 用于集成测试的 H2 以及用于质量保证和生产的 Postgresql 由于 spring boot 为您创建了默认数据源 因此我没有为集成测试定义任何内容 我