【问题标题】:Spring Application data source configuration from external file来自外部文件的 Spring Application 数据源配置
【发布时间】:2018-09-08 17:44:25
【问题描述】:

是否可以通过从Spring应用程序中未与WAR捆绑的外部文件中读取值来创建数据源。

【问题讨论】:

    标签: spring spring-data datasource spring-config


    【解决方案1】:

    您可以使用@PropertySource 注释来加载您的数据库属性,您可以从文件位置加载属性,如下所示:-

    @PropertySource("file:${app.home}/db.properties")
    

    参考链接:-

    https://www.mkyong.com/spring/spring-propertysources-example/

    对于基于 XML 的配置示例代码如下:-

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>${app.home}/db.properties</value>
            </list>
        </property>
    </bean>
    

    【讨论】:

      【解决方案2】:

      您可以在任何属性文件中设置您的数据源,然后您必须在运行您的战争的 catalina.sh 中提供该文件的类路径。 不要忘记在您的应用程序中加载该属性文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-01
        • 2015-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多