PropertyPlaceholderConfigurer:我可以拥有动态位置值

jun*_*idp 5 java

现在我在我的xml文件中有这个:

         <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/dashboardsupervisor" />
    <property name="username" value="root" />
    <property name="password" value="${jdbc.password}" />
</bean>
Run Code Online (Sandbox Code Playgroud)

            <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
    <value>file:C:/jdbc.properties</value>
  </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

现在,我的问题是我不知道这个文件的确切位置(jdbc.properties),因为这个应用程序将在不同的计算机上运行,​​在某些地方它安装在c:中,有时可能在f:..所以如果我不知道这个文件的路径,无论如何我都能找到它.

谢谢

Evg*_*eev 14

您可以将文件位置定义为系统属性,例如-Dprops.file = file:c:/1.properties

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
    <value>$props.file</value>
  </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

要么

<context:property-placeholder location="${props.file}"/>
Run Code Online (Sandbox Code Playgroud)

或者您可以扫描文件系统

public class ScanningPropertyPlaceholderConfigurer extends org.springframework.beans.factory.config.PropertyPlaceholderConfigurer {
    @Override
       public void setLocation(Resource location) {
       File file = findFile(fileName);  // implement file finder
       super.setLocation(new FileSystemResource(file));
    }
}
Run Code Online (Sandbox Code Playgroud)