【问题标题】:Tomcat set external properties file for dbTomcat 为 db 设置外部属性文件
【发布时间】:2021-11-08 13:14:52
【问题描述】:

我正在运行 spring boot 2.1.0

我想将我的 db-properties.yml 文件放在我的 webapp 目录之外。

我在我的 tomcat bin 文件夹中创建了一个新的 setenv.sh

export spring_config_additional-location=/path/to/file/db-properties.yml

我已将文件 db-properties.yml 放在该路径中。

spring:
  datasource:
     username: username
     url: jdbc:oracle:thin:@192.168.122.2:1521:xe
     password: password

但我收到此错误:

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

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

有人可以帮忙吗?

【问题讨论】:

  • 你上面显示的不是.properties.yml文件
  • 感谢您的建议。我已更改为 .yml 并重新启动了 Tomcat,但错误消息仍然相同。请帮忙。谢谢。
  • 您应该导出SPRING_CONFIG_ADDITIONALLOCATION,它是文件的路径还是完整路径?如果它只是没有文件名的路径,除非您指示 Spring Boot 加载名为`db-properties` 和application 的文件,否则它将不会加载任何内容。
  • 我也试过输入文件名,但错误信息还是一样。
  • 这能回答你的问题吗? Don't work spring.config.additional-location

标签: spring-boot tomcat


【解决方案1】:

我能够通过在 spring boot 的主类中覆盖以下方法来解决同样的问题。

        /**
         * For External Properties
         */
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplicationBuilder) {
            logger.info("\nLoading properties as configured...\n");
            return springApplicationBuilder.sources(MainApplication.class).properties(getProperties());
        }

        static Properties getProperties() {
            Properties props = new Properties();
            props.put(YourConstantsFile.SPRING_CONFIG_LOCATION,
                    "file:///xxxx/xxxx/xxx/xxx.yml");
            return props;
        }

    This will serve the purpose when you deploy the application as a war as well.

    Along with this, for locally running I included the below snippet too.

    @PropertySources({ @PropertySource(value = "classpath:application.properties"),
        @PropertySource(value = "file://${your.custom.file}", ignoreResourceNotFound = false)
    })
    @EnableTransactionManagement
    @SpringBootApplication
    public class MainApplication extends SpringBootServletInitializer{}


    Application.properties.


    server-home=${catalina.home}/anyfolder/
    your.custom.file=${server-home}/xxx.yml

```

【讨论】:

    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 2015-06-26
    • 2017-10-26
    • 2019-03-24
    • 2015-09-24
    • 1970-01-01
    • 2010-11-25
    • 2021-03-14
    相关资源
    最近更新 更多