Java 使用spring获取当前工作目录
这是在运行时获取java应用程序当前工作目录的代码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
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>