0

I am using CXF to create a java web service.

i have a file path, e.g "C:\ftproot", need to be configurable so i want to put this path into a properties file e.g application.properties

but how can i read the properties file in my java code?

can anyone help?

Thanks

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
jojo
  • 13,583
  • 35
  • 90
  • 123

2 Answers2

0

You need to put the properties file in your resources or WEB-INF/classes

Then

Properties properties = new Properties();
try {
    properties.load(new FileInputStream("classpath:application.properties"));
} catch (IOException e) {
}

See Also

jmj
  • 237,923
  • 42
  • 401
  • 438
  • I wonder, is there any Spring hook that allows `classpath:` URL resolving? Generally, this URL should not work in Java (see [this post](http://stackoverflow.com/questions/7596155/not-able-to-load-properties-file-in-java/7596193#7596193)), unless you register the URL handler e.g. via [`URL.setURLStreamHandlerFactory()`](http://stackoverflow.com/questions/148350/how-to-register-url-handler-for-apache-commons-httpclient/148390#148390) or via `java.protocol.handler.pkgs` system property. – dma_k Nov 21 '11 at 16:25
0

Create a PropertyPlaceholderConfigurer for Spring (Refer to the API for options).

Example:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="searchSystemEnvironment" value="true"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="locations">
        <list>
            <value>classpath:build.properties</value>
            <value>classpath:other.properties</value>
        </list>
    </property>
</bean>

Assuming you have a property file.path in the property file and you are using component scanning you can then use:

@Value("file.path") private String filePath;

That will then be populated with the value of file.path in the property file (if the bean is created by Spring)

Or if you are creating your beans in XML:

<bean class="yourClassName">
    <property name="filePath" value="${file.path} />
</bean>
Wilhelm Kleu
  • 10,821
  • 4
  • 36
  • 48