使用spring获取当前工作目录

Sai*_*kat 5 java spring

这是在运行时获取Java应用程序的当前工作目录的代码.

String currentWorkingDirectory = System.getProperty("user.dir")+System.getProperty("file.separator");
Run Code Online (Sandbox Code Playgroud)

有没有办法使用spring-context xml配置它.

例如:

<bean id="csvReportGenerator" class="some.path.CSVReportGenerator">  
<constructor-arg name="outputFileName" value="${currentWorkingDirectory}/${reportOutputFileGeneric}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

san*_*hat 8

是的,你可以使用Spring表达式来完成它.见第6.4.1 文章

<property name="userDir" value="#{ systemProperties['user.dir'] }"/>
<property name="fileSep" value="#{ systemProperties['file.separator'] }"/>
Run Code Online (Sandbox Code Playgroud)