首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tomcat为db设置外部属性文件

Tomcat为db设置外部属性文件
EN

Stack Overflow用户
提问于 2021-09-13 08:18:14
回答 1查看 154关注 0票数 0

我正在运行SpringBoot2.1.0

我想将properties.yml ties.yml文件放在webapp目录的外部。

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

代码语言:javascript
复制
export spring_config_additional-location=/path/to/file/db-properties.yml

我把文件db-properties.yml . and放在那条路上。

代码语言:javascript
复制
spring:
  datasource:
     username: username
     url: jdbc:oracle:thin:@192.168.122.2:1521:xe
     password: password

但是我发现了一个错误:

代码语言:javascript
复制
***************************
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

有人能帮忙吗?

EN

回答 1

Stack Overflow用户

发布于 2021-11-10 02:57:07

我能够通过在spring引导的主类中重写下面的方法来解决同样的问题。

代码语言:javascript
复制
        /**
         * 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
代码语言:javascript
复制
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69159389

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档