Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用spring获取当前工作目录_Java_Spring - Fatal编程技术网

Java 使用spring获取当前工作目录

Java 使用spring获取当前工作目录,java,spring,Java,Spring,这是在运行时获取java应用程序当前工作目录的代码 String currentWorkingDirectory = System.getProperty("user.dir")+System.getProperty("file.separator"); 是否有任何方法可以使用spring上下文xml来配置它 例如: <bean id="csvReportGenerator" class="some.path.CSVReportGenerator"> <construct

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

String currentWorkingDirectory = System.getProperty("user.dir")+System.getProperty("file.separator");
是否有任何方法可以使用spring上下文xml来配置它

例如:

<bean id="csvReportGenerator" class="some.path.CSVReportGenerator">  
<constructor-arg name="outputFileName" value="${currentWorkingDirectory}/${reportOutputFileGeneric}"/>
</bean>

是的,您可以使用Spring表达式来实现。见第条第6.4.1节



您可以简单地使用
类路径:
,或者如果您在unix环境中部署(通常是这样),也可以使用
/
。比如说,
classpath:sample.properties
/sample.properties
spring context.xml
中,您可以使用

1)
classpath:filename.properties

2)
/filename.properties

3)
文件:./

对于
上下文xml的当前目录,
/
应该可以工作,但是对于工作目录,
文件:./
可以正常工作

例如


类路径:/shaharma.properties
./shaharma-custom.properties
这可能会有所帮助
<property name="userDir" value="#{ systemProperties['user.dir'] }"/>
<property name="fileSep" value="#{ systemProperties['file.separator'] }"/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:annotation-config />

    <bean id="properties"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="singleton" value="true" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <value>classpath:/shaharma.properties</value>
                <value>./shaharma-custom.properties</value>
            </list>
        </property>
    </bean>

</beans>