外部文件中的Spring应用程序数据源配置

外部文件中的Spring应用程序数据源配置,spring,spring-data,datasource,spring-config,Spring,Spring Data,Datasource,Spring Config,是否可以通过读取外部文件中的值来创建数据源,该文件未与spring应用程序中的WAR绑定。您可以使用@PropertySource注释来加载db属性,并且可以从文件位置加载属性,如下所示:- @PropertySource("file:${app.home}/db.properties") <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <pro

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

您可以使用
@PropertySource
注释来加载db属性,并且可以从文件位置加载属性,如下所示:-

@PropertySource("file:${app.home}/db.properties")
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>${app.home}/db.properties</value>
        </list>
    </property>
</bean>
此处的参考链接:-

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

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

${app.home}/db.properties

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