本文介绍了PropertyPlaceholderConfigurer:我可以拥有动态位置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < bean id =dataSource
class = org.springframework.jdbc.datasource.DriverManagerDataSource >
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =jdbc:mysql:// localhost:3306 / dashboardsupervisor/>
< property name =usernamevalue =root/>
< property name =passwordvalue =$ {jdbc.password}/>
< / bean>

 < bean class =org.springframework.beans.factory.config.PropertyPlaceholderConfigurer> 
< property name =location>
< value>文件:C:/jdbc.properties< / value>
< / property>
< / bean>

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

谢谢

解决方案

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

 < bean class =org.springframework.beans。 factory.config.PropertyPlaceholderConfigurer> 
< property name =location>
< value> $ props.file< / value>
< / property>
< / bean>

 < context:property-placeholder location =$ {props.file}/> 

或者你可以扫描文件系统

  class ScanningPropertyPlaceholderConfigurer extends org.springframework.beans.factory.config.PropertyPlaceholderConfigurer {

public void setFileName(String fileName)throws FileNotFoundException {
File file =的FindFile(文件名); //实现文件查找器
super.setLocation(new FileSystemResource(file));
}


right now i have this in my xml file:

         <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>

and

            <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
    <value>file:C:/jdbc.properties</value>
  </property>
</bean>

Now , my problem is that i do not know the exact location of this file(jdbc.properties) , as this application will going to run on different computers , in some places its installed in c: ,sometimes may be on f:.. So if i dont know the path of this file , if there is anyway i could find it .

thanks

解决方案

You can define the file location as system property, eg -Dprops.file=file:c:/1.properties

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
    <value>$props.file</value>
  </property>
</bean>

or

<context:property-placeholder location="${props.file}"/>

or you can scan the file system

class ScanningPropertyPlaceholderConfigurer extends org.springframework.beans.factory.config.PropertyPlaceholderConfigurer {

    public void setFileName(String fileName) throws FileNotFoundException {
        File file = findFile(fileName);  // implement file finder
        super.setLocation(new FileSystemResource(file));
    }

这篇关于PropertyPlaceholderConfigurer:我可以拥有动态位置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 19:58